Values API: split into private and public APIs
[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 <unistd.h>
35 #include <stdlib.h>
36 #include <popt.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <glib.h>
40 #include <inttypes.h>
41 #include <unistd.h>
42 #include <signal.h>
43 #include "babeltrace-cfg.h"
44 #include "babeltrace-cfg-cli-args.h"
45 #include "babeltrace-cfg-cli-args-default.h"
46
47 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
48 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
49 #define NSEC_PER_SEC 1000000000LL
50
51 /*
52 * Known environment variable names for the log levels of the project's
53 * modules.
54 */
55 static const char* log_level_env_var_names[] = {
56 "BABELTRACE_COMMON_LOG_LEVEL",
57 "BABELTRACE_COMPAT_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
59 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
60 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
61 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
64 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
65 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
66 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
67 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
68 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
69 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
70 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
71 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
72 NULL,
73 };
74
75 /* Application's processing graph (weak) */
76 static struct bt_graph *the_graph;
77 static struct bt_query_executor *the_query_executor;
78 static bool canceled = false;
79
80 GPtrArray *loaded_plugins;
81
82 #ifdef __MINGW32__
83 #include <windows.h>
84
85 static
86 BOOL WINAPI signal_handler(DWORD signal) {
87 if (the_graph) {
88 bt_graph_cancel(the_graph);
89 }
90
91 canceled = true;
92
93 return TRUE;
94 }
95
96 static
97 void set_signal_handler(void)
98 {
99 if (!SetConsoleCtrlHandler(signal_handler, TRUE)) {
100 BT_LOGE("Failed to set the ctrl+c handler.");
101 }
102 }
103 #else /* __MINGW32__ */
104 static
105 void signal_handler(int signum)
106 {
107 if (signum != SIGINT) {
108 return;
109 }
110
111 if (the_graph) {
112 bt_graph_cancel(the_graph);
113 }
114
115 if (the_query_executor) {
116 bt_query_executor_cancel(the_query_executor);
117 }
118
119 canceled = true;
120 }
121
122 static
123 void set_signal_handler(void)
124 {
125 struct sigaction new_action, old_action;
126
127 new_action.sa_handler = signal_handler;
128 sigemptyset(&new_action.sa_mask);
129 new_action.sa_flags = 0;
130 sigaction(SIGINT, NULL, &old_action);
131
132 if (old_action.sa_handler != SIG_IGN) {
133 sigaction(SIGINT, &new_action, NULL);
134 }
135 }
136 #endif /* __MINGW32__ */
137
138 static
139 void init_static_data(void)
140 {
141 loaded_plugins = g_ptr_array_new_with_free_func(bt_object_put_ref);
142 }
143
144 static
145 void fini_static_data(void)
146 {
147 g_ptr_array_free(loaded_plugins, TRUE);
148 }
149
150 static
151 int create_the_query_executor(void)
152 {
153 int ret = 0;
154
155 the_query_executor = bt_query_executor_create();
156 if (!the_query_executor) {
157 BT_LOGE_STR("Cannot create a query executor.");
158 ret = -1;
159 }
160
161 return ret;
162 }
163
164 static
165 void destroy_the_query_executor(void)
166 {
167 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor);
168 }
169
170 static
171 int query(struct bt_component_class *comp_cls, const char *obj,
172 struct bt_value *params, struct bt_value **user_result,
173 const char **fail_reason)
174 {
175 struct bt_value *result = NULL;
176 enum bt_query_status status;
177 *fail_reason = "unknown error";
178 int ret = 0;
179
180 BT_ASSERT(fail_reason);
181 BT_ASSERT(user_result);
182 ret = create_the_query_executor();
183 if (ret) {
184 /* create_the_query_executor() logs errors */
185 goto end;
186 }
187
188 if (canceled) {
189 BT_LOGI("Canceled by user before executing the query: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls,
192 bt_component_class_get_name(comp_cls), obj);
193 *fail_reason = "canceled by user";
194 goto error;
195 }
196
197 while (true) {
198 status = bt_query_executor_query(the_query_executor, comp_cls,
199 obj, params, &result);
200 switch (status) {
201 case BT_QUERY_STATUS_OK:
202 goto ok;
203 case BT_QUERY_STATUS_AGAIN:
204 {
205 const uint64_t sleep_time_us = 100000;
206
207 /* Wait 100 ms and retry */
208 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
209 "time-us=%" PRIu64, sleep_time_us);
210
211 if (usleep(sleep_time_us)) {
212 if (bt_query_executor_is_canceled(the_query_executor)) {
213 BT_LOGI("Query was canceled by user: "
214 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
215 "query-obj=\"%s\"", comp_cls,
216 bt_component_class_get_name(comp_cls),
217 obj);
218 *fail_reason = "canceled by user";
219 goto error;
220 }
221 }
222
223 continue;
224 }
225 case BT_QUERY_STATUS_EXECUTOR_CANCELED:
226 *fail_reason = "canceled by user";
227 goto error;
228 case BT_QUERY_STATUS_ERROR:
229 case BT_QUERY_STATUS_INVALID:
230 goto error;
231 case BT_QUERY_STATUS_INVALID_OBJECT:
232 *fail_reason = "invalid or unknown query object";
233 goto error;
234 case BT_QUERY_STATUS_INVALID_PARAMS:
235 *fail_reason = "invalid query parameters";
236 goto error;
237 case BT_QUERY_STATUS_NOMEM:
238 *fail_reason = "not enough memory";
239 goto error;
240 default:
241 BT_LOGF("Unknown query status: status=%d", status);
242 abort();
243 }
244 }
245
246 ok:
247 *user_result = result;
248 result = NULL;
249 goto end;
250
251 error:
252 ret = -1;
253
254 end:
255 destroy_the_query_executor();
256 bt_object_put_ref(result);
257 return ret;
258 }
259
260 static
261 struct bt_plugin *find_plugin(const char *name)
262 {
263 int i;
264 struct bt_plugin *plugin = NULL;
265
266 BT_ASSERT(name);
267 BT_LOGD("Finding plugin: name=\"%s\"", name);
268
269 for (i = 0; i < loaded_plugins->len; i++) {
270 plugin = g_ptr_array_index(loaded_plugins, i);
271
272 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
273 break;
274 }
275
276 plugin = NULL;
277 }
278
279 if (BT_LOG_ON_DEBUG) {
280 if (plugin) {
281 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
282 } else {
283 BT_LOGD("Cannot find plugin.");
284 }
285 }
286
287 return bt_object_get_ref(plugin);
288 }
289
290 static
291 struct bt_component_class *find_component_class(const char *plugin_name,
292 const char *comp_class_name,
293 enum bt_component_class_type comp_class_type)
294 {
295 struct bt_component_class *comp_class = NULL;
296 struct bt_plugin *plugin;
297
298 BT_LOGD("Finding component class: plugin-name=\"%s\", "
299 "comp-cls-name=\"%s\", comp-cls-type=%d",
300 plugin_name, comp_class_name, comp_class_type);
301
302 plugin = find_plugin(plugin_name);
303
304 if (!plugin) {
305 goto end;
306 }
307
308 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
309 comp_class_name, comp_class_type);
310 BT_OBJECT_PUT_REF_AND_RESET(plugin);
311
312 end:
313 if (BT_LOG_ON_DEBUG) {
314 if (comp_class) {
315 BT_LOGD("Found component class: comp-cls-addr=%p",
316 comp_class);
317 } else {
318 BT_LOGD("Cannot find component class.");
319 }
320 }
321
322 return comp_class;
323 }
324
325 static
326 void print_indent(FILE *fp, size_t indent)
327 {
328 size_t i;
329
330 for (i = 0; i < indent; i++) {
331 fprintf(fp, " ");
332 }
333 }
334
335 static
336 const char *component_type_str(enum bt_component_class_type type)
337 {
338 switch (type) {
339 case BT_COMPONENT_CLASS_TYPE_SOURCE:
340 return "source";
341 case BT_COMPONENT_CLASS_TYPE_SINK:
342 return "sink";
343 case BT_COMPONENT_CLASS_TYPE_FILTER:
344 return "filter";
345 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
346 default:
347 return "(unknown)";
348 }
349 }
350
351 static
352 void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
353 const char *comp_cls_name, enum bt_component_class_type type)
354 {
355 GString *shell_plugin_name = NULL;
356 GString *shell_comp_cls_name = NULL;
357
358 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
359 if (!shell_plugin_name) {
360 goto end;
361 }
362
363 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
364 if (!shell_comp_cls_name) {
365 goto end;
366 }
367
368 fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'",
369 bt_common_color_bold(),
370 bt_common_color_fg_cyan(),
371 component_type_str(type),
372 bt_common_color_fg_default(),
373 bt_common_color_fg_blue(),
374 shell_plugin_name->str,
375 bt_common_color_fg_default(),
376 bt_common_color_fg_yellow(),
377 shell_comp_cls_name->str,
378 bt_common_color_reset());
379
380 end:
381 if (shell_plugin_name) {
382 g_string_free(shell_plugin_name, TRUE);
383 }
384
385 if (shell_comp_cls_name) {
386 g_string_free(shell_comp_cls_name, TRUE);
387 }
388 }
389
390 static
391 void print_value(FILE *, struct bt_value *, size_t);
392
393 static
394 void print_value_rec(FILE *, struct bt_value *, size_t);
395
396 struct print_map_value_data {
397 size_t indent;
398 FILE *fp;
399 };
400
401 static
402 bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
403 {
404 struct print_map_value_data *print_map_value_data = data;
405
406 print_indent(print_map_value_data->fp, print_map_value_data->indent);
407 fprintf(print_map_value_data->fp, "%s: ", key);
408 BT_ASSERT(object);
409
410 if (bt_value_is_array(object) &&
411 bt_value_array_is_empty(object)) {
412 fprintf(print_map_value_data->fp, "[ ]\n");
413 return true;
414 }
415
416 if (bt_value_is_map(object) &&
417 bt_value_map_is_empty(object)) {
418 fprintf(print_map_value_data->fp, "{ }\n");
419 return true;
420 }
421
422 if (bt_value_is_array(object) ||
423 bt_value_is_map(object)) {
424 fprintf(print_map_value_data->fp, "\n");
425 }
426
427 print_value_rec(print_map_value_data->fp, object,
428 print_map_value_data->indent + 2);
429 return BT_TRUE;
430 }
431
432 static
433 void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
434 {
435 bt_bool bool_val;
436 int64_t int_val;
437 double dbl_val;
438 const char *str_val;
439 int size;
440 int i;
441 enum bt_value_status status;
442
443 if (!value) {
444 return;
445 }
446
447 switch (bt_value_get_type(value)) {
448 case BT_VALUE_TYPE_NULL:
449 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
450 bt_common_color_reset());
451 break;
452 case BT_VALUE_TYPE_BOOL:
453 status = bt_value_bool_get(value, &bool_val);
454 if (status != BT_VALUE_STATUS_OK) {
455 goto error;
456 }
457 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
458 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
459 bt_common_color_reset());
460 break;
461 case BT_VALUE_TYPE_INTEGER:
462 status = bt_value_integer_get(value, &int_val);
463 if (status != BT_VALUE_STATUS_OK) {
464 goto error;
465 }
466 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
467 bt_common_color_fg_red(), int_val,
468 bt_common_color_reset());
469 break;
470 case BT_VALUE_TYPE_REAL:
471 status = bt_value_real_get(value, &dbl_val);
472 if (status != BT_VALUE_STATUS_OK) {
473 goto error;
474 }
475 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
476 bt_common_color_fg_red(), dbl_val,
477 bt_common_color_reset());
478 break;
479 case BT_VALUE_TYPE_STRING:
480 status = bt_value_string_get(value, &str_val);
481 if (status != BT_VALUE_STATUS_OK) {
482 goto error;
483 }
484 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
485 bt_common_color_fg_green(), str_val,
486 bt_common_color_reset());
487 break;
488 case BT_VALUE_TYPE_ARRAY:
489 size = bt_value_array_get_size(value);
490 if (size < 0) {
491 goto error;
492 }
493
494 if (size == 0) {
495 print_indent(fp, indent);
496 fprintf(fp, "[ ]\n");
497 break;
498 }
499
500 for (i = 0; i < size; i++) {
501 struct bt_value *element =
502 bt_value_array_borrow_element_by_index(
503 value, i);
504
505 if (!element) {
506 goto error;
507 }
508 print_indent(fp, indent);
509 fprintf(fp, "- ");
510
511 if (bt_value_is_array(element) &&
512 bt_value_array_is_empty(element)) {
513 fprintf(fp, "[ ]\n");
514 continue;
515 }
516
517 if (bt_value_is_map(element) &&
518 bt_value_map_is_empty(element)) {
519 fprintf(fp, "{ }\n");
520 continue;
521 }
522
523 if (bt_value_is_array(element) ||
524 bt_value_is_map(element)) {
525 fprintf(fp, "\n");
526 }
527
528 print_value_rec(fp, element, indent + 2);
529 }
530 break;
531 case BT_VALUE_TYPE_MAP:
532 {
533 struct print_map_value_data data = {
534 .indent = indent,
535 .fp = fp,
536 };
537
538 if (bt_value_map_is_empty(value)) {
539 print_indent(fp, indent);
540 fprintf(fp, "{ }\n");
541 break;
542 }
543
544 bt_value_map_foreach_entry(value, print_map_value, &data);
545 break;
546 }
547 default:
548 abort();
549 }
550 return;
551
552 error:
553 BT_LOGE("Error printing value of type %s.",
554 bt_common_value_type_string(bt_value_get_type(value)));
555 }
556
557 static
558 void print_value(FILE *fp, struct bt_value *value, size_t indent)
559 {
560 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
561 print_indent(fp, indent);
562 }
563
564 print_value_rec(fp, value, indent);
565 }
566
567 static
568 void print_bt_config_component(struct bt_config_component *bt_config_component)
569 {
570 fprintf(stderr, " ");
571 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
572 bt_config_component->comp_cls_name->str,
573 bt_config_component->type);
574 fprintf(stderr, ":\n");
575
576 if (bt_config_component->instance_name->len > 0) {
577 fprintf(stderr, " Name: %s\n",
578 bt_config_component->instance_name->str);
579 }
580
581 fprintf(stderr, " Parameters:\n");
582 print_value(stderr,
583 bt_value_borrow_from_private(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(bt_value_borrow_from_private(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(bt_value_borrow_from_private(cfg->plugin_paths));
643 }
644
645 static
646 void print_cfg_help(struct bt_config *cfg)
647 {
648 print_plugin_paths(bt_value_borrow_from_private(cfg->plugin_paths));
649 }
650
651 static
652 void print_cfg_print_ctf_metadata(struct bt_config *cfg)
653 {
654 print_plugin_paths(bt_value_borrow_from_private(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(bt_value_borrow_from_private(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(bt_value_borrow_from_private(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 bt_value_borrow_from_private(cfg->cmd_data.query.cfg_component->params),
924 &results, &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, bt_value_borrow_from_private(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_private_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_private_value_map_create();
1178 if (!params) {
1179 goto error;
1180 }
1181
1182 ret = bt_private_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", bt_value_borrow_from_private(params),
1189 &results, &fail_reason);
1190 if (ret) {
1191 goto failed;
1192 }
1193
1194 BT_ASSERT(results);
1195
1196 if (!bt_value_is_array(results)) {
1197 BT_LOGE_STR("Expecting an array for sessions query.");
1198 fprintf(stderr, "%s%sUnexpected type returned by session query%s\n",
1199 bt_common_color_bold(),
1200 bt_common_color_fg_red(),
1201 bt_common_color_reset());
1202 goto error;
1203 }
1204
1205 if (cfg->cmd_data.print_lttng_live_sessions.output_path->len > 0) {
1206 out_stream =
1207 fopen(cfg->cmd_data.print_lttng_live_sessions.output_path->str,
1208 "w");
1209 if (!out_stream) {
1210 ret = -1;
1211 BT_LOGE_ERRNO("Cannot open file for writing",
1212 ": path=\"%s\"",
1213 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
1214 goto end;
1215 }
1216 }
1217
1218 array_size = bt_value_array_get_size(results);
1219 for (i = 0; i < array_size; i++) {
1220 const char *url_text;
1221 int64_t timer_us, streams, clients;
1222
1223 map = bt_value_array_borrow_element_by_index(results, i);
1224 if (!map) {
1225 BT_LOGE_STR("Unexpected empty array entry.");
1226 goto error;
1227 }
1228 if (!bt_value_is_map(map)) {
1229 BT_LOGE_STR("Unexpected entry type.");
1230 goto error;
1231 }
1232
1233 v = bt_value_map_borrow_entry_value(map, "url");
1234 if (!v) {
1235 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1236 goto error;
1237 }
1238 ret = bt_value_string_get(v, &url_text);
1239 BT_ASSERT(ret == 0);
1240 fprintf(out_stream, "%s", url_text);
1241 v = bt_value_map_borrow_entry_value(map, "timer-us");
1242 if (!v) {
1243 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1244 goto error;
1245 }
1246 ret = bt_value_integer_get(v, &timer_us);
1247 BT_ASSERT(ret == 0);
1248 fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us);
1249 v = bt_value_map_borrow_entry_value(map, "stream-count");
1250 if (!v) {
1251 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1252 goto error;
1253 }
1254 ret = bt_value_integer_get(v, &streams);
1255 BT_ASSERT(ret == 0);
1256 fprintf(out_stream, "%" PRIu64 " stream(s), ", streams);
1257 v = bt_value_map_borrow_entry_value(map, "client-count");
1258 if (!v) {
1259 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1260 goto error;
1261 }
1262 ret = bt_value_integer_get(v, &clients);
1263 BT_ASSERT(ret == 0);
1264 fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients);
1265 }
1266
1267 goto end;
1268
1269 failed:
1270 BT_LOGE("Failed to query for sessions: %s", fail_reason);
1271 fprintf(stderr, "%s%sFailed to request sessions: %s%s\n",
1272 bt_common_color_bold(),
1273 bt_common_color_fg_red(),
1274 fail_reason,
1275 bt_common_color_reset());
1276
1277 error:
1278 ret = -1;
1279
1280 end:
1281 bt_object_put_ref(results);
1282 bt_object_put_ref(params);
1283 bt_object_put_ref(comp_cls);
1284
1285 if (out_stream && out_stream != stdout) {
1286 int fclose_ret = fclose(out_stream);
1287
1288 if (fclose_ret) {
1289 BT_LOGE_ERRNO("Cannot close file stream",
1290 ": path=\"%s\"",
1291 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
1292 }
1293 }
1294
1295 return 0;
1296 }
1297
1298 static
1299 int cmd_print_ctf_metadata(struct bt_config *cfg)
1300 {
1301 int ret = 0;
1302 struct bt_component_class *comp_cls = NULL;
1303 struct bt_value *results = NULL;
1304 struct bt_private_value *params = NULL;
1305 struct bt_value *metadata_text_value = NULL;
1306 const char *metadata_text = NULL;
1307 static const char * const plugin_name = "ctf";
1308 static const char * const comp_cls_name = "fs";
1309 static const enum bt_component_class_type comp_cls_type =
1310 BT_COMPONENT_CLASS_TYPE_SOURCE;
1311 const char *fail_reason = NULL;
1312 FILE *out_stream = stdout;
1313
1314 BT_ASSERT(cfg->cmd_data.print_ctf_metadata.path);
1315 comp_cls = find_component_class(plugin_name, comp_cls_name,
1316 comp_cls_type);
1317 if (!comp_cls) {
1318 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1319 "comp-cls-name=\"%s\", comp-cls-type=%d",
1320 plugin_name, comp_cls_name,
1321 BT_COMPONENT_CLASS_TYPE_SOURCE);
1322 fprintf(stderr, "%s%sCannot find component class %s",
1323 bt_common_color_bold(),
1324 bt_common_color_fg_red(),
1325 bt_common_color_reset());
1326 print_plugin_comp_cls_opt(stderr, plugin_name,
1327 comp_cls_name, comp_cls_type);
1328 fprintf(stderr, "\n");
1329 ret = -1;
1330 goto end;
1331 }
1332
1333 params = bt_private_value_map_create();
1334 if (!params) {
1335 ret = -1;
1336 goto end;
1337 }
1338
1339 ret = bt_private_value_map_insert_string_entry(params, "path",
1340 cfg->cmd_data.print_ctf_metadata.path->str);
1341 if (ret) {
1342 ret = -1;
1343 goto end;
1344 }
1345
1346 ret = query(comp_cls, "metadata-info",
1347 bt_value_borrow_from_private(params), &results, &fail_reason);
1348 if (ret) {
1349 goto failed;
1350 }
1351
1352 metadata_text_value = bt_value_map_borrow_entry_value(results, "text");
1353 if (!metadata_text_value) {
1354 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1355 ret = -1;
1356 goto end;
1357 }
1358
1359 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1360 BT_ASSERT(ret == 0);
1361
1362 if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) {
1363 out_stream =
1364 fopen(cfg->cmd_data.print_ctf_metadata.output_path->str,
1365 "w");
1366 if (!out_stream) {
1367 ret = -1;
1368 BT_LOGE_ERRNO("Cannot open file for writing",
1369 ": path=\"%s\"",
1370 cfg->cmd_data.print_ctf_metadata.output_path->str);
1371 goto end;
1372 }
1373 }
1374
1375 ret = fprintf(out_stream, "%s\n", metadata_text);
1376 if (ret < 0) {
1377 BT_LOGE("Cannot write whole metadata text to output stream: "
1378 "ret=%d", ret);
1379 }
1380
1381 goto end;
1382
1383 failed:
1384 ret = -1;
1385 BT_LOGE("Failed to query for metadata info: %s", fail_reason);
1386 fprintf(stderr, "%s%sFailed to request metadata info: %s%s\n",
1387 bt_common_color_bold(),
1388 bt_common_color_fg_red(),
1389 fail_reason,
1390 bt_common_color_reset());
1391
1392 end:
1393 destroy_the_query_executor();
1394 bt_object_put_ref(results);
1395 bt_object_put_ref(params);
1396 bt_object_put_ref(comp_cls);
1397
1398 if (out_stream && out_stream != stdout) {
1399 int fclose_ret = fclose(out_stream);
1400
1401 if (fclose_ret) {
1402 BT_LOGE_ERRNO("Cannot close file stream",
1403 ": path=\"%s\"",
1404 cfg->cmd_data.print_ctf_metadata.output_path->str);
1405 }
1406 }
1407
1408 return 0;
1409 }
1410
1411 struct port_id {
1412 char *instance_name;
1413 char *port_name;
1414 };
1415
1416 struct trace_range {
1417 uint64_t intersection_range_begin_ns;
1418 uint64_t intersection_range_end_ns;
1419 };
1420
1421 static
1422 guint port_id_hash(gconstpointer v)
1423 {
1424 const struct port_id *id = v;
1425
1426 BT_ASSERT(id->instance_name);
1427 BT_ASSERT(id->port_name);
1428
1429 return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name);
1430 }
1431
1432 static
1433 gboolean port_id_equal(gconstpointer v1, gconstpointer v2)
1434 {
1435 const struct port_id *id1 = v1;
1436 const struct port_id *id2 = v2;
1437
1438 return !strcmp(id1->instance_name, id2->instance_name) &&
1439 !strcmp(id1->port_name, id2->port_name);
1440 }
1441
1442 static
1443 void port_id_destroy(gpointer data)
1444 {
1445 struct port_id *id = data;
1446
1447 free(id->instance_name);
1448 free(id->port_name);
1449 free(id);
1450 }
1451
1452 static
1453 void trace_range_destroy(gpointer data)
1454 {
1455 free(data);
1456 }
1457
1458 struct cmd_run_ctx {
1459 /* Owned by this */
1460 GHashTable *components;
1461
1462 /* Owned by this */
1463 struct bt_graph *graph;
1464
1465 /* Weak */
1466 struct bt_config *cfg;
1467
1468 bool connect_ports;
1469
1470 bool stream_intersection_mode;
1471
1472 /*
1473 * Association of struct port_id -> struct trace_range.
1474 */
1475 GHashTable *intersections;
1476 };
1477
1478 /* Returns a timestamp of the form "(-)s.ns" */
1479 static
1480 char *s_from_ns(int64_t ns)
1481 {
1482 int ret;
1483 char *s_ret = NULL;
1484 bool is_negative;
1485 int64_t ts_sec_abs, ts_nsec_abs;
1486 int64_t ts_sec = ns / NSEC_PER_SEC;
1487 int64_t ts_nsec = ns % NSEC_PER_SEC;
1488
1489 if (ts_sec >= 0 && ts_nsec >= 0) {
1490 is_negative = false;
1491 ts_sec_abs = ts_sec;
1492 ts_nsec_abs = ts_nsec;
1493 } else if (ts_sec > 0 && ts_nsec < 0) {
1494 is_negative = false;
1495 ts_sec_abs = ts_sec - 1;
1496 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
1497 } else if (ts_sec == 0 && ts_nsec < 0) {
1498 is_negative = true;
1499 ts_sec_abs = ts_sec;
1500 ts_nsec_abs = -ts_nsec;
1501 } else if (ts_sec < 0 && ts_nsec > 0) {
1502 is_negative = true;
1503 ts_sec_abs = -(ts_sec + 1);
1504 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
1505 } else if (ts_sec < 0 && ts_nsec == 0) {
1506 is_negative = true;
1507 ts_sec_abs = -ts_sec;
1508 ts_nsec_abs = ts_nsec;
1509 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1510 is_negative = true;
1511 ts_sec_abs = -ts_sec;
1512 ts_nsec_abs = -ts_nsec;
1513 }
1514
1515 ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64,
1516 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1517 if (ret < 0) {
1518 s_ret = NULL;
1519 }
1520 return s_ret;
1521 }
1522
1523 static
1524 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1525 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1526 struct bt_port *upstream_port,
1527 struct bt_config_connection *cfg_conn)
1528 {
1529 int ret = 0;
1530 GQuark downstreamp_comp_name_quark;
1531 struct bt_component *downstream_comp;
1532 int64_t downstream_port_count;
1533 uint64_t i;
1534 int64_t (*port_count_fn)(struct bt_component *);
1535 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
1536 enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
1537 bool insert_trimmer = false;
1538 struct bt_private_value *trimmer_params = NULL;
1539 char *intersection_begin = NULL;
1540 char *intersection_end = NULL;
1541 struct bt_component *trimmer = NULL;
1542 struct bt_component_class *trimmer_class = NULL;
1543 struct bt_port *trimmer_input = NULL;
1544 struct bt_port *trimmer_output = NULL;
1545
1546 if (ctx->intersections &&
1547 bt_component_get_class_type(upstream_comp) ==
1548 BT_COMPONENT_CLASS_TYPE_SOURCE) {
1549 struct trace_range *range;
1550 struct port_id port_id = {
1551 .instance_name = (char *) bt_component_get_name(upstream_comp),
1552 .port_name = (char *) bt_port_get_name(upstream_port)
1553 };
1554
1555 if (!port_id.instance_name || !port_id.port_name) {
1556 goto error;
1557 }
1558
1559 range = (struct trace_range *) g_hash_table_lookup(
1560 ctx->intersections, &port_id);
1561 if (range) {
1562 enum bt_value_status status;
1563
1564 intersection_begin = s_from_ns(
1565 range->intersection_range_begin_ns);
1566 intersection_end = s_from_ns(
1567 range->intersection_range_end_ns);
1568 if (!intersection_begin || !intersection_end) {
1569 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1570 goto error;
1571 }
1572
1573 insert_trimmer = true;
1574 trimmer_params = bt_private_value_map_create();
1575 if (!trimmer_params) {
1576 goto error;
1577 }
1578
1579 status = bt_private_value_map_insert_string_entry(
1580 trimmer_params, "begin", intersection_begin);
1581 if (status != BT_VALUE_STATUS_OK) {
1582 goto error;
1583 }
1584 status = bt_private_value_map_insert_string_entry(
1585 trimmer_params,
1586 "end", intersection_end);
1587 if (status != BT_VALUE_STATUS_OK) {
1588 goto error;
1589 }
1590 }
1591
1592 trimmer_class = find_component_class("utils", "trimmer",
1593 BT_COMPONENT_CLASS_TYPE_FILTER);
1594 if (!trimmer_class) {
1595 goto error;
1596 }
1597 }
1598
1599 BT_LOGI("Connecting upstream port to the next available downstream port: "
1600 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1601 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1602 upstream_port, bt_port_get_name(upstream_port),
1603 cfg_conn->downstream_comp_name->str,
1604 cfg_conn->arg->str);
1605 downstreamp_comp_name_quark = g_quark_from_string(
1606 cfg_conn->downstream_comp_name->str);
1607 BT_ASSERT(downstreamp_comp_name_quark > 0);
1608 downstream_comp = g_hash_table_lookup(ctx->components,
1609 GUINT_TO_POINTER(downstreamp_comp_name_quark));
1610 if (!downstream_comp) {
1611 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1612 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1613 cfg_conn->arg->str);
1614 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1615 cfg_conn->arg->str);
1616 goto error;
1617 }
1618
1619 if (bt_component_is_filter(downstream_comp)) {
1620 port_count_fn = bt_component_filter_get_input_port_count;
1621 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1622 } else if (bt_component_is_sink(downstream_comp)) {
1623 port_count_fn = bt_component_sink_get_input_port_count;
1624 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1625 } else {
1626 /*
1627 * Should never happen because the connections are
1628 * validated before we get here.
1629 */
1630 BT_LOGF("Invalid connection: downstream component is a source: "
1631 "conn-arg=\"%s\"", cfg_conn->arg->str);
1632 abort();
1633 }
1634
1635 downstream_port_count = port_count_fn(downstream_comp);
1636 BT_ASSERT(downstream_port_count >= 0);
1637
1638 for (i = 0; i < downstream_port_count; i++) {
1639 struct bt_port *downstream_port =
1640 port_by_index_fn(downstream_comp, i);
1641 const char *upstream_port_name;
1642 const char *downstream_port_name;
1643
1644 BT_ASSERT(downstream_port);
1645
1646 /* Skip port if it's already connected. */
1647 if (bt_port_is_connected(downstream_port)) {
1648 bt_object_put_ref(downstream_port);
1649 BT_LOGD("Skipping downstream port: already connected: "
1650 "port-addr=%p, port-name=\"%s\"",
1651 downstream_port,
1652 bt_port_get_name(downstream_port));
1653 continue;
1654 }
1655
1656 downstream_port_name = bt_port_get_name(downstream_port);
1657 BT_ASSERT(downstream_port_name);
1658 upstream_port_name = bt_port_get_name(upstream_port);
1659 BT_ASSERT(upstream_port_name);
1660
1661 if (!bt_common_star_glob_match(
1662 cfg_conn->downstream_port_glob->str, SIZE_MAX,
1663 downstream_port_name, SIZE_MAX)) {
1664 bt_object_put_ref(downstream_port);
1665 continue;
1666 }
1667
1668 if (insert_trimmer) {
1669 /*
1670 * In order to insert the trimmer between the two
1671 * components that were being connected, we create
1672 * a connection configuration entry which describes
1673 * a connection from the trimmer's output to the
1674 * original input that was being connected.
1675 *
1676 * Hence, the creation of the trimmer will cause the
1677 * graph "new port" listener to establish all downstream
1678 * connections as its output port is connected. We will
1679 * then establish the connection between the original
1680 * upstream source and the trimmer.
1681 */
1682 char *trimmer_name = NULL;
1683 enum bt_graph_status graph_status;
1684
1685 ret = asprintf(&trimmer_name, "%s-%s",
1686 "stream-intersection-trimmer",
1687 upstream_port_name);
1688 if (ret < 0) {
1689 goto error;
1690 }
1691 ret = 0;
1692
1693 ctx->connect_ports = false;
1694 graph_status = bt_graph_add_component(ctx->graph,
1695 trimmer_class, trimmer_name,
1696 bt_value_borrow_from_private(trimmer_params),
1697 &trimmer);
1698 free(trimmer_name);
1699 if (graph_status != BT_GRAPH_STATUS_OK) {
1700 goto error;
1701 }
1702 BT_ASSERT(trimmer);
1703
1704 trimmer_input =
1705 bt_component_filter_get_input_port_by_index(
1706 trimmer, 0);
1707 if (!trimmer_input) {
1708 goto error;
1709 }
1710 trimmer_output =
1711 bt_component_filter_get_output_port_by_index(
1712 trimmer, 0);
1713 if (!trimmer_output) {
1714 goto error;
1715 }
1716
1717 /*
1718 * Replace the current downstream port by the trimmer's
1719 * upstream port.
1720 */
1721 BT_OBJECT_MOVE_REF(downstream_port, trimmer_input);
1722 downstream_port_name = bt_port_get_name(
1723 downstream_port);
1724 if (!downstream_port_name) {
1725 goto error;
1726 }
1727 }
1728
1729 /* We have a winner! */
1730 status = bt_graph_connect_ports(ctx->graph,
1731 upstream_port, downstream_port, NULL);
1732 BT_OBJECT_PUT_REF_AND_RESET(downstream_port);
1733 switch (status) {
1734 case BT_GRAPH_STATUS_OK:
1735 break;
1736 case BT_GRAPH_STATUS_CANCELED:
1737 BT_LOGI_STR("Graph was canceled by user.");
1738 status = BT_GRAPH_STATUS_OK;
1739 break;
1740 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
1741 BT_LOGE("A component refused a connection to one of its ports: "
1742 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1743 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1744 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1745 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1746 "conn-arg=\"%s\"",
1747 upstream_comp, bt_component_get_name(upstream_comp),
1748 upstream_port, bt_port_get_name(upstream_port),
1749 downstream_comp, cfg_conn->downstream_comp_name->str,
1750 downstream_port, downstream_port_name,
1751 cfg_conn->arg->str);
1752 fprintf(stderr,
1753 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1754 bt_port_get_name(upstream_port),
1755 downstream_port_name,
1756 cfg_conn->arg->str);
1757 break;
1758 default:
1759 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1760 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1761 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1762 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1763 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1764 "conn-arg=\"%s\"",
1765 upstream_comp, bt_component_get_name(upstream_comp),
1766 upstream_port, bt_port_get_name(upstream_port),
1767 downstream_comp, cfg_conn->downstream_comp_name->str,
1768 downstream_port, downstream_port_name,
1769 cfg_conn->arg->str);
1770 fprintf(stderr,
1771 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1772 bt_port_get_name(upstream_port),
1773 downstream_port_name,
1774 cfg_conn->arg->str);
1775 goto error;
1776 }
1777
1778 BT_LOGI("Connected component ports: "
1779 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1780 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1781 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1782 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1783 "conn-arg=\"%s\"",
1784 upstream_comp, bt_component_get_name(upstream_comp),
1785 upstream_port, bt_port_get_name(upstream_port),
1786 downstream_comp, cfg_conn->downstream_comp_name->str,
1787 downstream_port, downstream_port_name,
1788 cfg_conn->arg->str);
1789
1790 if (insert_trimmer) {
1791 /*
1792 * The first connection, from the source to the trimmer,
1793 * has been done. We now connect the trimmer to the
1794 * original downstream port.
1795 */
1796 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1797 ctx, trimmer, trimmer_output, cfg_conn);
1798 if (ret) {
1799 goto error;
1800 }
1801 ctx->connect_ports = true;
1802 }
1803
1804 /*
1805 * We found a matching downstream port: the search is
1806 * over.
1807 */
1808 goto end;
1809 }
1810
1811 /* No downstream port found */
1812 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1813 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1814 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1815 upstream_port, bt_port_get_name(upstream_port),
1816 cfg_conn->downstream_comp_name->str,
1817 cfg_conn->arg->str);
1818 fprintf(stderr,
1819 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1820 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1821
1822 error:
1823 ret = -1;
1824
1825 end:
1826 free(intersection_begin);
1827 free(intersection_end);
1828 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params);
1829 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class);
1830 BT_OBJECT_PUT_REF_AND_RESET(trimmer);
1831 BT_OBJECT_PUT_REF_AND_RESET(trimmer_input);
1832 BT_OBJECT_PUT_REF_AND_RESET(trimmer_output);
1833 return ret;
1834 }
1835
1836 static
1837 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1838 struct bt_port *upstream_port)
1839 {
1840 int ret = 0;
1841 const char *upstream_port_name;
1842 const char *upstream_comp_name;
1843 struct bt_component *upstream_comp = NULL;
1844 size_t i;
1845
1846 BT_ASSERT(ctx);
1847 BT_ASSERT(upstream_port);
1848 upstream_port_name = bt_port_get_name(upstream_port);
1849 BT_ASSERT(upstream_port_name);
1850 upstream_comp = bt_port_get_component(upstream_port);
1851 if (!upstream_comp) {
1852 BT_LOGW("Upstream port to connect is not part of a component: "
1853 "port-addr=%p, port-name=\"%s\"",
1854 upstream_port, upstream_port_name);
1855 ret = -1;
1856 goto end;
1857 }
1858
1859 upstream_comp_name = bt_component_get_name(upstream_comp);
1860 BT_ASSERT(upstream_comp_name);
1861 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1862 "port-addr=%p, port-name=\"%s\"",
1863 upstream_comp, upstream_comp_name,
1864 upstream_port, upstream_port_name);
1865
1866 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1867 struct bt_config_connection *cfg_conn =
1868 g_ptr_array_index(
1869 ctx->cfg->cmd_data.run.connections, i);
1870
1871 if (strcmp(cfg_conn->upstream_comp_name->str,
1872 upstream_comp_name)) {
1873 continue;
1874 }
1875
1876 if (!bt_common_star_glob_match(
1877 cfg_conn->upstream_port_glob->str,
1878 SIZE_MAX, upstream_port_name, SIZE_MAX)) {
1879 continue;
1880 }
1881
1882 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1883 ctx, upstream_comp, upstream_port, cfg_conn);
1884 if (ret) {
1885 BT_LOGE("Cannot connect upstream port: "
1886 "port-addr=%p, port-name=\"%s\"",
1887 upstream_port,
1888 upstream_port_name);
1889 fprintf(stderr,
1890 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1891 upstream_port_name,
1892 upstream_comp_name,
1893 cfg_conn->arg->str);
1894 goto error;
1895 }
1896 goto end;
1897 }
1898
1899 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1900 "port-addr=%p, port-name=\"%s\"", upstream_port,
1901 upstream_port_name);
1902 fprintf(stderr,
1903 "Cannot create connection: upstream port `%s` does not match any connection\n",
1904 upstream_port_name);
1905
1906 error:
1907 ret = -1;
1908
1909 end:
1910 bt_object_put_ref(upstream_comp);
1911 return ret;
1912 }
1913
1914 static
1915 void graph_port_added_listener(struct bt_port *port, void *data)
1916 {
1917 struct bt_component *comp = NULL;
1918 struct cmd_run_ctx *ctx = data;
1919
1920 comp = bt_port_get_component(port);
1921 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1922 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1923 comp, comp ? bt_component_get_name(comp) : "",
1924 port, bt_port_get_name(port));
1925
1926 if (!ctx->connect_ports) {
1927 goto end;
1928 }
1929
1930 if (!comp) {
1931 BT_LOGW_STR("Port has no component.");
1932 goto end;
1933 }
1934
1935 if (bt_port_is_connected(port)) {
1936 BT_LOGW_STR("Port is already connected.");
1937 goto end;
1938 }
1939
1940 if (!bt_port_is_output(port)) {
1941 BT_LOGI_STR("Skipping input port.");
1942 goto end;
1943 }
1944
1945 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
1946 BT_LOGF_STR("Cannot connect upstream port.");
1947 fprintf(stderr, "Added port could not be connected: aborting\n");
1948 abort();
1949 }
1950
1951 end:
1952 bt_object_put_ref(comp);
1953 return;
1954 }
1955
1956 static
1957 void graph_port_removed_listener(struct bt_component *component,
1958 struct bt_port *port, void *data)
1959 {
1960 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1961 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1962 component, bt_component_get_name(component),
1963 port, bt_port_get_name(port));
1964 }
1965
1966 static
1967 void graph_ports_connected_listener(struct bt_port *upstream_port,
1968 struct bt_port *downstream_port, void *data)
1969 {
1970 struct bt_component *upstream_comp = bt_port_get_component(upstream_port);
1971 struct bt_component *downstream_comp = bt_port_get_component(downstream_port);
1972
1973 BT_ASSERT(upstream_comp);
1974 BT_ASSERT(downstream_comp);
1975 BT_LOGI("Graph's component ports connected: "
1976 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1977 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1978 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1979 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1980 upstream_comp, bt_component_get_name(upstream_comp),
1981 upstream_port, bt_port_get_name(upstream_port),
1982 downstream_comp, bt_component_get_name(downstream_comp),
1983 downstream_port, bt_port_get_name(downstream_port));
1984 bt_object_put_ref(upstream_comp);
1985 bt_object_put_ref(downstream_comp);
1986 }
1987
1988 static
1989 void graph_ports_disconnected_listener(
1990 struct bt_component *upstream_component,
1991 struct bt_component *downstream_component,
1992 struct bt_port *upstream_port, struct bt_port *downstream_port,
1993 void *data)
1994 {
1995 BT_LOGI("Graph's component ports disconnected: "
1996 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1997 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1998 upstream_port, bt_port_get_name(upstream_port),
1999 downstream_port, bt_port_get_name(downstream_port));
2000 }
2001
2002 static
2003 void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
2004 {
2005 if (!ctx) {
2006 return;
2007 }
2008
2009 if (ctx->components) {
2010 g_hash_table_destroy(ctx->components);
2011 ctx->components = NULL;
2012 }
2013
2014 if (ctx->intersections) {
2015 g_hash_table_destroy(ctx->intersections);
2016 ctx->intersections = NULL;
2017 }
2018
2019 BT_OBJECT_PUT_REF_AND_RESET(ctx->graph);
2020 the_graph = NULL;
2021 ctx->cfg = NULL;
2022 }
2023
2024 static
2025 int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
2026 {
2027 int ret = 0;
2028
2029 ctx->cfg = cfg;
2030 ctx->connect_ports = false;
2031 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2032 NULL, bt_object_put_ref);
2033 if (!ctx->components) {
2034 goto error;
2035 }
2036
2037 if (cfg->cmd_data.run.stream_intersection_mode) {
2038 ctx->stream_intersection_mode = true;
2039 ctx->intersections = g_hash_table_new_full(port_id_hash,
2040 port_id_equal, port_id_destroy, trace_range_destroy);
2041 if (!ctx->intersections) {
2042 goto error;
2043 }
2044 }
2045
2046 ctx->graph = bt_graph_create();
2047 if (!ctx->graph) {
2048 goto error;
2049 }
2050
2051 the_graph = ctx->graph;
2052 ret = bt_graph_add_port_added_listener(ctx->graph,
2053 graph_port_added_listener, NULL, ctx);
2054 if (ret < 0) {
2055 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2056 goto error;
2057 }
2058
2059 ret = bt_graph_add_port_removed_listener(ctx->graph,
2060 graph_port_removed_listener, NULL, ctx);
2061 if (ret < 0) {
2062 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2063 goto error;
2064 }
2065
2066 ret = bt_graph_add_ports_connected_listener(ctx->graph,
2067 graph_ports_connected_listener, NULL, ctx);
2068 if (ret < 0) {
2069 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2070 goto error;
2071 }
2072
2073 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
2074 graph_ports_disconnected_listener, NULL, ctx);
2075 if (ret < 0) {
2076 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2077 goto error;
2078 }
2079
2080 goto end;
2081
2082 error:
2083 cmd_run_ctx_destroy(ctx);
2084 ret = -1;
2085
2086 end:
2087 return ret;
2088 }
2089
2090 static
2091 int set_stream_intersections(struct cmd_run_ctx *ctx,
2092 struct bt_config_component *cfg_comp,
2093 struct bt_component_class *comp_cls)
2094 {
2095 int ret = 0;
2096 uint64_t trace_idx;
2097 int64_t trace_count;
2098 enum bt_value_status value_status;
2099 const char *path = NULL;
2100 struct bt_value *component_path_value = NULL;
2101 struct bt_private_value *query_params = NULL;
2102 struct bt_value *query_result = NULL;
2103 struct bt_value *trace_info = NULL;
2104 struct bt_value *intersection_range = NULL;
2105 struct bt_value *intersection_begin = NULL;
2106 struct bt_value *intersection_end = NULL;
2107 struct bt_value *stream_path_value = NULL;
2108 struct bt_value *stream_paths = NULL;
2109 struct bt_value *stream_infos = NULL;
2110 struct bt_value *stream_info = NULL;
2111 struct port_id *port_id = NULL;
2112 struct trace_range *trace_range = NULL;
2113 const char *fail_reason = NULL;
2114
2115 component_path_value = bt_value_map_borrow_entry_value(
2116 bt_value_borrow_from_private(cfg_comp->params),
2117 "path");
2118 if (component_path_value && !bt_value_is_string(component_path_value)) {
2119 BT_LOGD("Cannot get path parameter: component-name=%s",
2120 cfg_comp->instance_name->str);
2121 ret = -1;
2122 goto error;
2123 }
2124
2125 value_status = bt_value_string_get(component_path_value, &path);
2126 if (value_status != BT_VALUE_STATUS_OK) {
2127 BT_LOGD("Cannot get path string value: component-name=%s",
2128 cfg_comp->instance_name->str);
2129 ret = -1;
2130 goto error;
2131 }
2132
2133 query_params = bt_private_value_map_create();
2134 if (!query_params) {
2135 BT_LOGE_STR("Cannot create query parameters.");
2136 ret = -1;
2137 goto error;
2138 }
2139
2140 value_status = bt_private_value_map_insert_entry(query_params, "path",
2141 component_path_value);
2142 if (value_status != BT_VALUE_STATUS_OK) {
2143 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2144 ret = -1;
2145 goto error;
2146 }
2147
2148 ret = query(comp_cls, "trace-info",
2149 bt_value_borrow_from_private(query_params), &query_result,
2150 &fail_reason);
2151 if (ret) {
2152 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2153 "comp-class-name=\"%s\"", fail_reason,
2154 bt_component_class_get_name(comp_cls));
2155 ret = -1;
2156 goto error;
2157 }
2158
2159 BT_ASSERT(query_result);
2160
2161 if (!bt_value_is_array(query_result)) {
2162 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2163 "component-class-name=%s",
2164 bt_component_class_get_name(comp_cls));
2165 ret = -1;
2166 goto error;
2167 }
2168
2169 trace_count = bt_value_array_get_size(query_result);
2170 if (trace_count < 0) {
2171 ret = -1;
2172 goto error;
2173 }
2174
2175 for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
2176 int64_t begin, end;
2177 uint64_t stream_idx;
2178 int64_t stream_count;
2179
2180 trace_info = bt_value_array_borrow_element_by_index(
2181 query_result, trace_idx);
2182 if (!trace_info || !bt_value_is_map(trace_info)) {
2183 ret = -1;
2184 BT_LOGD_STR("Cannot retrieve trace from query result.");
2185 goto error;
2186 }
2187
2188 intersection_range = bt_value_map_borrow_entry_value(trace_info,
2189 "intersection-range-ns");
2190 if (!intersection_range) {
2191 ret = -1;
2192 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2193 goto error;
2194 }
2195
2196 intersection_begin = bt_value_map_borrow_entry_value(
2197 intersection_range, "begin");
2198 if (!intersection_begin) {
2199 ret = -1;
2200 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2201 goto error;
2202 }
2203
2204 intersection_end = bt_value_map_borrow_entry_value(
2205 intersection_range, "end");
2206 if (!intersection_end) {
2207 ret = -1;
2208 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2209 goto error;
2210 }
2211
2212 value_status = bt_value_integer_get(intersection_begin, &begin);
2213 if (value_status != BT_VALUE_STATUS_OK) {
2214 ret = -1;
2215 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2216 goto error;
2217 }
2218
2219 value_status = bt_value_integer_get(intersection_end, &end);
2220 if (value_status != BT_VALUE_STATUS_OK) {
2221 ret = -1;
2222 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2223 goto error;
2224 }
2225
2226 if (begin < 0 || end < 0 || end < begin) {
2227 BT_LOGW("Invalid trace stream intersection values: "
2228 "intersection-range-ns:begin=%" PRId64
2229 ", intersection-range-ns:end=%" PRId64,
2230 begin, end);
2231 ret = -1;
2232 goto error;
2233 }
2234
2235 stream_infos = bt_value_map_borrow_entry_value(trace_info,
2236 "streams");
2237 if (!stream_infos || !bt_value_is_array(stream_infos)) {
2238 ret = -1;
2239 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2240 goto error;
2241 }
2242
2243 stream_count = bt_value_array_get_size(stream_infos);
2244 if (stream_count < 0) {
2245 ret = -1;
2246 goto error;
2247 }
2248
2249 /*
2250 * FIXME
2251 *
2252 * The first path of a stream's "paths" is currently used to
2253 * associate streams/ports to a given trace intersection.
2254 *
2255 * This is a fragile hack as it relies on the port names
2256 * being set to the various streams path.
2257 *
2258 * A stream name should be introduced as part of the trace-info
2259 * query result.
2260 */
2261 for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
2262 const char *stream_path;
2263
2264 port_id = g_new0(struct port_id, 1);
2265 if (!port_id) {
2266 ret = -1;
2267 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2268 goto error;
2269 }
2270 port_id->instance_name = strdup(cfg_comp->instance_name->str);
2271 if (!port_id->instance_name) {
2272 ret = -1;
2273 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2274 goto error;
2275 }
2276
2277 trace_range = g_new0(struct trace_range, 1);
2278 if (!trace_range) {
2279 ret = -1;
2280 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2281 goto error;
2282 }
2283 trace_range->intersection_range_begin_ns = begin;
2284 trace_range->intersection_range_end_ns = end;
2285
2286 stream_info = bt_value_array_borrow_element_by_index(
2287 stream_infos, stream_idx);
2288 if (!stream_info || !bt_value_is_map(stream_info)) {
2289 ret = -1;
2290 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2291 goto error;
2292 }
2293
2294 stream_paths = bt_value_map_borrow_entry_value(
2295 stream_info, "paths");
2296 if (!stream_paths || !bt_value_is_array(stream_paths)) {
2297 ret = -1;
2298 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2299 goto error;
2300 }
2301
2302 stream_path_value =
2303 bt_value_array_borrow_element_by_index(
2304 stream_paths, 0);
2305 if (!stream_path_value ||
2306 !bt_value_is_string(stream_path_value)) {
2307 ret = -1;
2308 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2309 goto error;
2310 }
2311
2312 value_status = bt_value_string_get(stream_path_value,
2313 &stream_path);
2314 if (value_status != BT_VALUE_STATUS_OK) {
2315 ret = -1;
2316 goto error;
2317 }
2318
2319 port_id->port_name = strdup(stream_path);
2320 if (!port_id->port_name) {
2321 ret = -1;
2322 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2323 goto error;
2324 }
2325
2326 BT_LOGD("Inserting stream intersection ");
2327
2328 g_hash_table_insert(ctx->intersections, port_id, trace_range);
2329
2330 port_id = NULL;
2331 trace_range = NULL;
2332 }
2333 }
2334
2335 goto end;
2336
2337 error:
2338 fprintf(stderr, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2339 bt_common_color_bold(),
2340 bt_common_color_fg_yellow(),
2341 path ? path : "(unknown)",
2342 bt_common_color_reset());
2343 end:
2344 bt_object_put_ref(query_params);
2345 bt_object_put_ref(query_result);
2346 g_free(port_id);
2347 g_free(trace_range);
2348 return ret;
2349 }
2350
2351 static
2352 int cmd_run_ctx_create_components_from_config_components(
2353 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
2354 {
2355 size_t i;
2356 struct bt_component_class *comp_cls = NULL;
2357 struct bt_component *comp = NULL;
2358 int ret = 0;
2359
2360 for (i = 0; i < cfg_components->len; i++) {
2361 struct bt_config_component *cfg_comp =
2362 g_ptr_array_index(cfg_components, i);
2363 GQuark quark;
2364
2365 comp_cls = find_component_class(cfg_comp->plugin_name->str,
2366 cfg_comp->comp_cls_name->str, cfg_comp->type);
2367 if (!comp_cls) {
2368 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2369 "comp-cls-name=\"%s\", comp-cls-type=%d",
2370 cfg_comp->plugin_name->str,
2371 cfg_comp->comp_cls_name->str,
2372 cfg_comp->type);
2373 fprintf(stderr, "%s%sCannot find component class %s",
2374 bt_common_color_bold(),
2375 bt_common_color_fg_red(),
2376 bt_common_color_reset());
2377 print_plugin_comp_cls_opt(stderr,
2378 cfg_comp->plugin_name->str,
2379 cfg_comp->comp_cls_name->str,
2380 cfg_comp->type);
2381 fprintf(stderr, "\n");
2382 goto error;
2383 }
2384
2385 ret = bt_graph_add_component(ctx->graph, comp_cls,
2386 cfg_comp->instance_name->str,
2387 bt_value_borrow_from_private(cfg_comp->params), &comp);
2388 if (ret) {
2389 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2390 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2391 "comp-name=\"%s\"",
2392 cfg_comp->plugin_name->str,
2393 cfg_comp->comp_cls_name->str,
2394 cfg_comp->type, cfg_comp->instance_name->str);
2395 fprintf(stderr, "%s%sCannot create component `%s`%s\n",
2396 bt_common_color_bold(),
2397 bt_common_color_fg_red(),
2398 cfg_comp->instance_name->str,
2399 bt_common_color_reset());
2400 goto error;
2401 }
2402
2403 if (ctx->stream_intersection_mode &&
2404 cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
2405 ret = set_stream_intersections(ctx, cfg_comp, comp_cls);
2406 if (ret) {
2407 goto error;
2408 }
2409 }
2410
2411 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2412 comp, cfg_comp->instance_name->str);
2413 quark = g_quark_from_string(cfg_comp->instance_name->str);
2414 BT_ASSERT(quark > 0);
2415 g_hash_table_insert(ctx->components,
2416 GUINT_TO_POINTER(quark), comp);
2417 comp = NULL;
2418 BT_OBJECT_PUT_REF_AND_RESET(comp_cls);
2419 }
2420
2421 goto end;
2422
2423 error:
2424 ret = -1;
2425
2426 end:
2427 bt_object_put_ref(comp);
2428 bt_object_put_ref(comp_cls);
2429 return ret;
2430 }
2431
2432 static
2433 int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
2434 {
2435 int ret = 0;
2436
2437 /*
2438 * Make sure that, during this phase, our graph's "port added"
2439 * listener does not connect ports while we are creating the
2440 * components because we have a special, initial phase for
2441 * this.
2442 */
2443 ctx->connect_ports = false;
2444
2445 ret = cmd_run_ctx_create_components_from_config_components(
2446 ctx, ctx->cfg->cmd_data.run.sources);
2447 if (ret) {
2448 ret = -1;
2449 goto end;
2450 }
2451
2452 ret = cmd_run_ctx_create_components_from_config_components(
2453 ctx, ctx->cfg->cmd_data.run.filters);
2454 if (ret) {
2455 ret = -1;
2456 goto end;
2457 }
2458
2459 ret = cmd_run_ctx_create_components_from_config_components(
2460 ctx, ctx->cfg->cmd_data.run.sinks);
2461 if (ret) {
2462 ret = -1;
2463 goto end;
2464 }
2465
2466 end:
2467 return ret;
2468 }
2469
2470 static
2471 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
2472 struct bt_component *comp,
2473 int64_t (*port_count_fn)(struct bt_component *),
2474 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t))
2475 {
2476 int ret = 0;
2477 int64_t count;
2478 uint64_t i;
2479
2480 count = port_count_fn(comp);
2481 BT_ASSERT(count >= 0);
2482
2483 for (i = 0; i < count; i++) {
2484 struct bt_port *upstream_port = port_by_index_fn(comp, i);
2485
2486 BT_ASSERT(upstream_port);
2487 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
2488 bt_object_put_ref(upstream_port);
2489 if (ret) {
2490 goto end;
2491 }
2492 }
2493
2494 end:
2495 return ret;
2496 }
2497
2498 static
2499 int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
2500 {
2501 int ret = 0;
2502 GHashTableIter iter;
2503 gpointer g_name_quark, g_comp;
2504
2505 ctx->connect_ports = true;
2506 g_hash_table_iter_init(&iter, ctx->components);
2507
2508 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
2509 int64_t (*port_count_fn)(struct bt_component *);
2510 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
2511
2512 if (bt_component_is_source(g_comp)) {
2513 port_count_fn =
2514 bt_component_source_get_output_port_count;
2515 port_by_index_fn =
2516 bt_component_source_get_output_port_by_index;
2517 } else if (bt_component_is_filter(g_comp)) {
2518 port_count_fn =
2519 bt_component_filter_get_output_port_count;
2520 port_by_index_fn =
2521 bt_component_filter_get_output_port_by_index;
2522 } else {
2523 continue;
2524 }
2525
2526 ret = cmd_run_ctx_connect_comp_ports(ctx,
2527 g_comp, port_count_fn, port_by_index_fn);
2528 if (ret) {
2529 goto end;
2530 }
2531 }
2532
2533 end:
2534 return ret;
2535 }
2536
2537 static inline
2538 const char *bt_graph_status_str(enum bt_graph_status status)
2539 {
2540 switch (status) {
2541 case BT_GRAPH_STATUS_CANCELED:
2542 return "BT_GRAPH_STATUS_CANCELED";
2543 case BT_GRAPH_STATUS_AGAIN:
2544 return "BT_GRAPH_STATUS_AGAIN";
2545 case BT_GRAPH_STATUS_END:
2546 return "BT_GRAPH_STATUS_END";
2547 case BT_GRAPH_STATUS_OK:
2548 return "BT_GRAPH_STATUS_OK";
2549 case BT_GRAPH_STATUS_INVALID:
2550 return "BT_GRAPH_STATUS_INVALID";
2551 case BT_GRAPH_STATUS_NO_SINK:
2552 return "BT_GRAPH_STATUS_NO_SINK";
2553 case BT_GRAPH_STATUS_ERROR:
2554 return "BT_GRAPH_STATUS_ERROR";
2555 default:
2556 return "(unknown)";
2557 }
2558 }
2559
2560 static
2561 int cmd_run(struct bt_config *cfg)
2562 {
2563 int ret = 0;
2564 struct cmd_run_ctx ctx = { 0 };
2565
2566 /* Initialize the command's context and the graph object */
2567 if (cmd_run_ctx_init(&ctx, cfg)) {
2568 BT_LOGE_STR("Cannot initialize the command's context.");
2569 fprintf(stderr, "Cannot initialize the command's context\n");
2570 goto error;
2571 }
2572
2573 if (canceled) {
2574 BT_LOGI_STR("Canceled by user before creating components.");
2575 goto error;
2576 }
2577
2578 BT_LOGI_STR("Creating components.");
2579
2580 /* Create the requested component instances */
2581 if (cmd_run_ctx_create_components(&ctx)) {
2582 BT_LOGE_STR("Cannot create components.");
2583 fprintf(stderr, "Cannot create components\n");
2584 goto error;
2585 }
2586
2587 if (canceled) {
2588 BT_LOGI_STR("Canceled by user before connecting components.");
2589 goto error;
2590 }
2591
2592 BT_LOGI_STR("Connecting components.");
2593
2594 /* Connect the initially visible component ports */
2595 if (cmd_run_ctx_connect_ports(&ctx)) {
2596 BT_LOGE_STR("Cannot connect initial component ports.");
2597 fprintf(stderr, "Cannot connect initial component ports\n");
2598 goto error;
2599 }
2600
2601 if (canceled) {
2602 BT_LOGI_STR("Canceled by user before running the graph.");
2603 goto error;
2604 }
2605
2606 BT_LOGI_STR("Running the graph.");
2607
2608 /* Run the graph */
2609 while (true) {
2610 enum bt_graph_status graph_status = bt_graph_run(ctx.graph);
2611
2612 /*
2613 * Reset console in case something messed with console
2614 * codes during the graph's execution.
2615 */
2616 printf("%s", bt_common_color_reset());
2617 fflush(stdout);
2618 fprintf(stderr, "%s", bt_common_color_reset());
2619 BT_LOGV("bt_graph_run() returned: status=%s",
2620 bt_graph_status_str(graph_status));
2621
2622 switch (graph_status) {
2623 case BT_GRAPH_STATUS_OK:
2624 break;
2625 case BT_GRAPH_STATUS_CANCELED:
2626 BT_LOGI_STR("Graph was canceled by user.");
2627 goto error;
2628 case BT_GRAPH_STATUS_AGAIN:
2629 if (bt_graph_is_canceled(ctx.graph)) {
2630 BT_LOGI_STR("Graph was canceled by user.");
2631 goto error;
2632 }
2633
2634 if (cfg->cmd_data.run.retry_duration_us > 0) {
2635 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2636 "time-us=%" PRIu64,
2637 cfg->cmd_data.run.retry_duration_us);
2638
2639 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
2640 if (bt_graph_is_canceled(ctx.graph)) {
2641 BT_LOGI_STR("Graph was canceled by user.");
2642 goto error;
2643 }
2644 }
2645 }
2646 break;
2647 case BT_COMPONENT_STATUS_END:
2648 goto end;
2649 default:
2650 BT_LOGE_STR("Graph failed to complete successfully");
2651 fprintf(stderr, "Graph failed to complete successfully\n");
2652 goto error;
2653 }
2654 }
2655
2656 goto end;
2657
2658 error:
2659 if (ret == 0) {
2660 ret = -1;
2661 }
2662
2663 end:
2664 cmd_run_ctx_destroy(&ctx);
2665 return ret;
2666 }
2667
2668 static
2669 void warn_command_name_and_directory_clash(struct bt_config *cfg)
2670 {
2671 const char *env_clash;
2672
2673 if (!cfg->command_name) {
2674 return;
2675 }
2676
2677 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
2678 if (env_clash && strcmp(env_clash, "0") == 0) {
2679 return;
2680 }
2681
2682 if (g_file_test(cfg->command_name,
2683 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
2684 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2685 cfg->command_name);
2686 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
2687 cfg->command_name);
2688 fprintf(stderr, "\n");
2689 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
2690 cfg->command_name);
2691 }
2692 }
2693
2694 static
2695 void init_log_level(void)
2696 {
2697 bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL);
2698 }
2699
2700 static
2701 void set_auto_log_levels(struct bt_config *cfg)
2702 {
2703 const char **env_var_name;
2704
2705 /*
2706 * Override the configuration's default log level if
2707 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2708 * are found for backward compatibility with legacy Babetrace 1.
2709 */
2710 if (getenv("BABELTRACE_DEBUG") &&
2711 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2712 cfg->log_level = 'V';
2713 } else if (getenv("BABELTRACE_VERBOSE") &&
2714 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2715 cfg->log_level = 'I';
2716 }
2717
2718 /*
2719 * Set log levels according to --debug or --verbose. For
2720 * backward compatibility, --debug is more verbose than
2721 * --verbose. So:
2722 *
2723 * --verbose: INFO log level
2724 * --debug: VERBOSE log level (includes DEBUG, which is
2725 * is less verbose than VERBOSE in the internal
2726 * logging framework)
2727 */
2728 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2729 if (cfg->verbose) {
2730 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
2731 } else if (cfg->debug) {
2732 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
2733 } else {
2734 /*
2735 * Set library's default log level if not
2736 * explicitly specified.
2737 */
2738 switch (cfg->log_level) {
2739 case 'N':
2740 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE);
2741 break;
2742 case 'V':
2743 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
2744 break;
2745 case 'D':
2746 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG);
2747 break;
2748 case 'I':
2749 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
2750 break;
2751 case 'W':
2752 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN);
2753 break;
2754 case 'E':
2755 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR);
2756 break;
2757 case 'F':
2758 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL);
2759 break;
2760 default:
2761 abort();
2762 }
2763 }
2764 }
2765
2766 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) {
2767 if (cfg->verbose) {
2768 bt_cli_log_level = BT_LOG_INFO;
2769 } else if (cfg->debug) {
2770 bt_cli_log_level = BT_LOG_VERBOSE;
2771 } else {
2772 /*
2773 * Set CLI's default log level if not explicitly
2774 * specified.
2775 */
2776 switch (cfg->log_level) {
2777 case 'N':
2778 bt_cli_log_level = BT_LOG_NONE;
2779 break;
2780 case 'V':
2781 bt_cli_log_level = BT_LOG_VERBOSE;
2782 break;
2783 case 'D':
2784 bt_cli_log_level = BT_LOG_DEBUG;
2785 break;
2786 case 'I':
2787 bt_cli_log_level = BT_LOG_INFO;
2788 break;
2789 case 'W':
2790 bt_cli_log_level = BT_LOG_WARN;
2791 break;
2792 case 'E':
2793 bt_cli_log_level = BT_LOG_ERROR;
2794 break;
2795 case 'F':
2796 bt_cli_log_level = BT_LOG_FATAL;
2797 break;
2798 default:
2799 abort();
2800 }
2801 }
2802 }
2803
2804 env_var_name = log_level_env_var_names;
2805
2806 while (*env_var_name) {
2807 if (!getenv(*env_var_name)) {
2808 if (cfg->verbose) {
2809 g_setenv(*env_var_name, "I", 1);
2810 } else if (cfg->debug) {
2811 g_setenv(*env_var_name, "V", 1);
2812 } else {
2813 char val[2] = { 0 };
2814
2815 /*
2816 * Set module's default log level if not
2817 * explicitly specified.
2818 */
2819 val[0] = cfg->log_level;
2820 g_setenv(*env_var_name, val, 1);
2821 }
2822 }
2823
2824 env_var_name++;
2825 }
2826 }
2827
2828 int main(int argc, const char **argv)
2829 {
2830 int ret;
2831 int retcode;
2832 struct bt_config *cfg;
2833
2834 init_log_level();
2835 set_signal_handler();
2836 init_static_data();
2837 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
2838
2839 if (retcode < 0) {
2840 /* Quit without errors; typically usage/version */
2841 retcode = 0;
2842 BT_LOGI_STR("Quitting without errors.");
2843 goto end;
2844 }
2845
2846 if (retcode > 0) {
2847 BT_LOGE("Command-line error: retcode=%d", retcode);
2848 goto end;
2849 }
2850
2851 if (!cfg) {
2852 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2853 fprintf(stderr, "Failed to create Babeltrace configuration\n");
2854 retcode = 1;
2855 goto end;
2856 }
2857
2858 set_auto_log_levels(cfg);
2859 print_cfg(cfg);
2860
2861 if (cfg->command_needs_plugins) {
2862 ret = load_all_plugins(
2863 bt_value_borrow_from_private(cfg->plugin_paths));
2864 if (ret) {
2865 BT_LOGE("Failed to load plugins: ret=%d", ret);
2866 retcode = 1;
2867 goto end;
2868 }
2869 }
2870
2871 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2872 cfg->command, cfg->command_name);
2873
2874 switch (cfg->command) {
2875 case BT_CONFIG_COMMAND_RUN:
2876 ret = cmd_run(cfg);
2877 break;
2878 case BT_CONFIG_COMMAND_LIST_PLUGINS:
2879 ret = cmd_list_plugins(cfg);
2880 break;
2881 case BT_CONFIG_COMMAND_HELP:
2882 ret = cmd_help(cfg);
2883 break;
2884 case BT_CONFIG_COMMAND_QUERY:
2885 ret = cmd_query(cfg);
2886 break;
2887 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
2888 ret = cmd_print_ctf_metadata(cfg);
2889 break;
2890 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
2891 ret = cmd_print_lttng_live_sessions(cfg);
2892 break;
2893 default:
2894 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
2895 abort();
2896 }
2897
2898 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2899 cfg->command, cfg->command_name, ret);
2900 warn_command_name_and_directory_clash(cfg);
2901 retcode = ret ? 1 : 0;
2902
2903 end:
2904 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2905 fini_static_data();
2906 return retcode;
2907 }
This page took 0.132718 seconds and 4 git commands to generate.