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