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