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