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