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