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