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