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