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