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