+ goto end;
+ }
+
+ printf("From the following plugin paths:\n\n");
+ print_value(cfg->plugin_paths, 2);
+ printf("\n");
+ plugins_count = loaded_plugins->len;
+ if (plugins_count == 0) {
+ fprintf(stderr, "%s%sNo plugins found.%s\n",
+ bt_common_color_bold(), bt_common_color_fg_red(),
+ bt_common_color_reset());
+ fprintf(stderr, "\n");
+ fprintf(stderr, "Please make sure your plugin search path is set correctly. You can use\n");
+ fprintf(stderr, "the --plugin-path command-line option or the BABELTRACE_PLUGIN_PATH\n");
+ fprintf(stderr, "environment variable.\n");
+ ret = -1;
+ goto end;
+ }
+
+ for (i = 0; i < plugins_count; i++) {
+ struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
+
+ component_classes_count += bt_plugin_get_component_class_count(plugin);
+ }
+
+ printf("Found %s%d%s component classes in %s%d%s plugins.\n",
+ bt_common_color_bold(),
+ component_classes_count,
+ bt_common_color_reset(),
+ bt_common_color_bold(),
+ plugins_count,
+ bt_common_color_reset());
+
+ for (i = 0; i < plugins_count; i++) {
+ int j;
+ struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
+
+ component_classes_count =
+ bt_plugin_get_component_class_count(plugin);
+ printf("\n");
+ print_plugin_info(plugin);
+
+ if (component_classes_count == 0) {
+ printf(" %sComponent classes%s: (None)\n",
+ bt_common_color_bold(),
+ bt_common_color_reset());
+ } else {
+ printf(" %sComponent classes%s:\n",
+ bt_common_color_bold(),
+ bt_common_color_reset());
+ }
+
+ for (j = 0; j < component_classes_count; j++) {
+ struct bt_component_class *comp_class =
+ bt_plugin_get_component_class(plugin, j);
+ const char *comp_class_name =
+ bt_component_class_get_name(comp_class);
+ const char *comp_class_description =
+ bt_component_class_get_description(comp_class);
+ enum bt_component_class_type type =
+ bt_component_class_get_type(comp_class);
+
+ printf(" ");
+ print_plugin_comp_cls_opt(stdout,
+ bt_plugin_get_name(plugin), comp_class_name,
+ type);
+
+ if (comp_class_description) {
+ printf(": %s", comp_class_description);
+ }
+
+ printf("\n");
+ bt_put(comp_class);
+ }
+ }
+
+end:
+ return ret;
+}
+
+static int cmd_print_lttng_live_sessions(struct bt_config *cfg)
+{
+ printf("TODO\n");
+ return -1;
+}
+
+static int cmd_print_ctf_metadata(struct bt_config *cfg)
+{
+ int ret = 0;
+ struct bt_component_class *comp_cls = NULL;
+ struct bt_value *results = NULL;
+ struct bt_value *params = NULL;
+ struct bt_value *metadata_text_value = NULL;
+ const char *metadata_text = NULL;
+ static const char * const plugin_name = "ctf";
+ static const char * const comp_cls_name = "fs";
+ static const enum bt_component_class_type comp_cls_type =
+ BT_COMPONENT_CLASS_TYPE_SOURCE;
+
+ assert(cfg->cmd_data.print_ctf_metadata.path);
+ comp_cls = find_component_class(plugin_name, comp_cls_name,
+ comp_cls_type);
+ if (!comp_cls) {
+ fprintf(stderr, "%s%sCannot find component class %s",
+ bt_common_color_bold(),
+ bt_common_color_fg_red(),
+ bt_common_color_reset());
+ print_plugin_comp_cls_opt(stderr, plugin_name,
+ comp_cls_name, comp_cls_type);
+ fprintf(stderr, "\n");
+ ret = -1;
+ goto end;
+ }
+
+ params = bt_value_map_create();
+ if (!params) {
+ ret = -1;
+ goto end;
+ }
+
+ ret = bt_value_map_insert_string(params, "path",
+ cfg->cmd_data.print_ctf_metadata.path->str);
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+ results = bt_component_class_query(comp_cls, "metadata-info",
+ params);
+ if (!results) {
+ ret = -1;
+ fprintf(stderr, "%s%sFailed to request metadata info%s\n",
+ bt_common_color_bold(),
+ bt_common_color_fg_red(),
+ bt_common_color_reset());
+ goto end;
+ }
+
+ metadata_text_value = bt_value_map_get(results, "text");
+ if (!metadata_text_value) {
+ ret = -1;
+ goto end;
+ }
+
+ ret = bt_value_string_get(metadata_text_value, &metadata_text);
+ assert(ret == 0);
+ printf("%s\n", metadata_text);
+
+end:
+ bt_put(results);
+ bt_put(params);
+ bt_put(metadata_text_value);
+ bt_put(comp_cls);
+ return 0;
+}
+
+static int cmd_run(struct bt_config *cfg)
+{
+ int ret = 0;
+ struct bt_component_class *source_class = NULL;
+ struct bt_component_class *sink_class = NULL;
+ struct bt_component *source = NULL, *sink = NULL;
+ struct bt_value *source_params = NULL, *sink_params = NULL;
+ struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
+ struct bt_graph *graph = NULL;
+
+ ret = load_all_plugins(cfg->plugin_paths);
+ if (ret) {
+ goto end;
+ }
+
+ /* TODO handle more than 1 source and 1 sink. */
+ if (cfg->cmd_data.run.sources->len != 1 ||
+ cfg->cmd_data.run.sinks->len != 1) {
+ fprintf(stderr, "Only one source and one sink component class are supported. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ source_cfg = bt_config_get_component(cfg->cmd_data.run.sources, 0);
+ source_params = bt_get(source_cfg->params);
+ source_class = find_component_class(source_cfg->plugin_name->str,
+ source_cfg->comp_cls_name->str,
+ BT_COMPONENT_CLASS_TYPE_SOURCE);
+ if (!source_class) {
+ fprintf(stderr, "Could not find ");
+ print_plugin_comp_cls_opt(stderr, source_cfg->plugin_name->str,
+ source_cfg->comp_cls_name->str, BT_COMPONENT_CLASS_TYPE_SOURCE);
+ fprintf(stderr, ". Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ sink_cfg = bt_config_get_component(cfg->cmd_data.run.sinks, 0);
+ sink_params = bt_get(sink_cfg->params);
+ sink_class = find_component_class(sink_cfg->plugin_name->str,
+ sink_cfg->comp_cls_name->str,
+ BT_COMPONENT_CLASS_TYPE_SINK);
+ if (!sink_class) {
+ fprintf(stderr, "Could not find ");
+ print_plugin_comp_cls_opt(stderr, sink_cfg->plugin_name->str,
+ sink_cfg->comp_cls_name->str, BT_COMPONENT_CLASS_TYPE_SINK);
+ fprintf(stderr, ". Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ graph = bt_graph_create();
+ if (!graph) {
+ ret = -1;
+ goto end;
+ }
+
+ source = bt_component_create(source_class, "source", source_params);
+ if (!source) {
+ fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ sink = bt_component_create(sink_class, "sink", sink_params);
+ if (!sink) {
+ fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ ret = connect_source_sink(graph, source, source_cfg, sink);
+ if (ret) {
+ ret = -1;
+ goto end;
+ }
+
+ while (true) {
+ enum bt_graph_status graph_status;
+
+ graph_status = bt_graph_run(graph);
+ switch (graph_status) {
+ case BT_GRAPH_STATUS_AGAIN:
+ /* Wait for an arbitraty 500 ms. */
+ usleep(500000);
+ break;
+ case BT_COMPONENT_STATUS_END:
+ goto end;
+ default:
+ fprintf(stderr, "Sink component returned an error, aborting...\n");
+ ret = -1;
+ goto end;
+ }
+ }
+
+end:
+ bt_put(sink_class);
+ bt_put(source_class);
+ bt_put(source);
+ bt_put(sink);
+ bt_put(source_params);
+ bt_put(sink_params);
+ bt_put(sink_cfg);
+ bt_put(source_cfg);
+ bt_put(graph);
+ return ret;
+}
+
+static void warn_command_name_and_directory_clash(struct bt_config *cfg)
+{
+ if (!cfg->command_name) {
+ return;
+ }
+
+ if (g_file_test(cfg->command_name,
+ G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
+ fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
+ cfg->command_name);
+ fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
+ cfg->command_name);
+ fprintf(stderr, "\n");
+ fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
+ cfg->command_name);
+ }
+}
+
+int main(int argc, const char **argv)
+{
+ int ret;
+ int retcode;
+ struct bt_config *cfg;
+
+ init_loaded_plugins_array();
+ cfg = bt_config_from_args_with_defaults(argc, argv, &retcode);
+
+ if (retcode < 0) {
+ /* Quit without errors; typically usage/version */
+ retcode = 0;
+ goto end;
+ }
+
+ if (retcode > 0) {
+ goto end;
+ }
+
+ if (!cfg) {
+ fprintf(stderr, "Failed to create Babeltrace configuration\n");
+ retcode = 1;
+ goto end;
+ }
+
+ babeltrace_debug = cfg->debug;
+ babeltrace_verbose = cfg->verbose;
+ print_cfg(cfg);
+
+ if (cfg->command_needs_plugins) {
+ ret = load_all_plugins(cfg->plugin_paths);
+ if (ret) {
+ retcode = 1;
+ goto end;
+ }
+ }
+
+ switch (cfg->command) {
+ case BT_CONFIG_COMMAND_RUN:
+ ret = cmd_run(cfg);
+ break;
+ case BT_CONFIG_COMMAND_LIST_PLUGINS:
+ ret = cmd_list_plugins(cfg);
+ break;
+ case BT_CONFIG_COMMAND_HELP:
+ ret = cmd_help(cfg);
+ break;
+ case BT_CONFIG_COMMAND_QUERY:
+ ret = cmd_query(cfg);
+ break;
+ case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
+ ret = cmd_print_ctf_metadata(cfg);
+ break;
+ case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
+ ret = cmd_print_lttng_live_sessions(cfg);
+ break;
+ default:
+ assert(false);
+ }
+
+ warn_command_name_and_directory_clash(cfg);
+ retcode = ret ? 1 : 0;
+
+end:
+ BT_PUT(cfg);
+ fini_loaded_plugins_array();
+ return retcode;