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