+/*
+ * Prints the help command usage.
+ */
+static
+void print_query_usage(FILE *fp)
+{
+ fprintf(fp, "Usage: babeltrace [GEN OPTS] query [OPTS] OBJECT --source=PLUGIN.COMPCLS\n");
+ fprintf(fp, " babeltrace [GEN OPTS] query [OPTS] OBJECT --filter=PLUGIN.COMPCLS\n");
+ fprintf(fp, " babeltrace [GEN OPTS] query [OPTS] OBJECT --sink=PLUGIN.COMPCLS\n");
+ fprintf(fp, "\n");
+ fprintf(fp, "Options:\n");
+ fprintf(fp, "\n");
+ fprintf(fp, " --filter=PLUGIN.COMPCLS Query object from the filter component\n");
+ fprintf(fp, " class COMPCLS found in the plugin PLUGIN\n");
+ fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
+ fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
+ fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
+ fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
+ fprintf(fp, " (see the expected format of PARAMS below)\n");
+ fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
+ fprintf(fp, " dynamic plugins can be loaded\n");
+ fprintf(fp, " --sink=PLUGIN.COMPCLS Query object from the sink component class\n");
+ fprintf(fp, " COMPCLS found in the plugin PLUGIN\n");
+ fprintf(fp, " --source=PLUGIN.COMPCLS Query object from the source component\n");
+ fprintf(fp, " class COMPCLS found in the plugin PLUGIN\n");
+ fprintf(fp, " -h --help Show this help and quit\n");
+ fprintf(fp, "\n\n");
+ print_expected_params_format(fp);
+}
+
+static struct poptOption query_long_options[] = {
+ /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
+ { "filter", '\0', POPT_ARG_STRING, NULL, OPT_FILTER, NULL, NULL },
+ { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
+ { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
+ { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
+ { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
+ { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
+ { "sink", '\0', POPT_ARG_STRING, NULL, OPT_SINK, NULL, NULL },
+ { "source", '\0', POPT_ARG_STRING, NULL, OPT_SOURCE, NULL, NULL },
+ { NULL, 0, 0, NULL, 0, NULL, NULL },
+};
+
+/*
+ * Creates a Babeltrace config object from the arguments of a query
+ * command.
+ *
+ * *retcode is set to the appropriate exit code to use.
+ */
+struct bt_config *bt_config_query_from_args(int argc, const char *argv[],
+ int *retcode, bool omit_system_plugin_path,
+ bool omit_home_plugin_path,
+ struct bt_value *initial_plugin_paths)
+{
+ poptContext pc = NULL;
+ char *arg = NULL;
+ int opt;
+ int ret;
+ struct bt_config *cfg = NULL;
+ const char *leftover;
+ struct bt_value *params = bt_value_null;
+
+ *retcode = 0;
+ cfg = bt_config_query_create(initial_plugin_paths);
+ if (!cfg) {
+ print_err_oom();
+ goto error;
+ }
+
+ cfg->cmd_data.query.omit_system_plugin_path =
+ omit_system_plugin_path;
+ cfg->cmd_data.query.omit_home_plugin_path = omit_home_plugin_path;
+ ret = append_env_var_plugin_paths(cfg->cmd_data.query.plugin_paths);
+ if (ret) {
+ printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
+ goto error;
+ }
+
+ /* Parse options */
+ pc = poptGetContext(NULL, argc, (const char **) argv,
+ query_long_options, 0);
+ if (!pc) {
+ printf_err("Cannot get popt context\n");
+ goto error;
+ }
+
+ poptReadDefaultConfig(pc, 0);
+
+ while ((opt = poptGetNextOpt(pc)) > 0) {
+ arg = poptGetOptArg(pc);
+
+ switch (opt) {
+ case OPT_PLUGIN_PATH:
+ if (bt_common_is_setuid_setgid()) {
+ printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
+ } else {
+ if (bt_config_append_plugin_paths(
+ cfg->cmd_data.query.plugin_paths,
+ arg)) {
+ printf_err("Invalid --plugin-path option's argument:\n %s\n",
+ arg);
+ goto error;
+ }
+ }
+ break;
+ case OPT_OMIT_SYSTEM_PLUGIN_PATH:
+ cfg->cmd_data.query.omit_system_plugin_path = true;
+ break;
+ case OPT_OMIT_HOME_PLUGIN_PATH:
+ cfg->cmd_data.query.omit_home_plugin_path = true;
+ break;
+ case OPT_SOURCE:
+ case OPT_FILTER:
+ case OPT_SINK:
+ {
+ enum bt_component_class_type type;
+
+ if (cfg->cmd_data.query.cfg_component) {
+ printf_err("Cannot specify more than one plugin and component class:\n %s\n",
+ arg);
+ goto error;
+ }
+
+ switch (opt) {
+ case OPT_SOURCE:
+ type = BT_COMPONENT_CLASS_TYPE_SOURCE;
+ break;
+ case OPT_FILTER:
+ type = BT_COMPONENT_CLASS_TYPE_FILTER;
+ break;
+ case OPT_SINK:
+ type = BT_COMPONENT_CLASS_TYPE_SINK;
+ break;
+ default:
+ assert(false);
+ }
+
+ cfg->cmd_data.query.cfg_component =
+ bt_config_component_from_arg(type, arg);
+ if (!cfg->cmd_data.query.cfg_component) {
+ printf_err("Invalid format for --source/--filter/--sink option's argument:\n %s\n",
+ arg);
+ goto error;
+ }
+
+ /* Default parameters: null */
+ bt_put(cfg->cmd_data.query.cfg_component->params);
+ cfg->cmd_data.query.cfg_component->params =
+ bt_value_null;
+ break;
+ }
+ case OPT_PARAMS:
+ {
+ params = bt_value_from_arg(arg);
+ if (!params) {
+ printf_err("Invalid format for --params option's argument:\n %s\n",
+ arg);
+ goto error;
+ }
+ break;
+ }
+ case OPT_HELP:
+ print_query_usage(stdout);
+ *retcode = -1;
+ BT_PUT(cfg);
+ goto end;
+ default:
+ printf_err("Unknown command-line option specified (option code %d)\n",
+ opt);
+ goto error;
+ }
+
+ free(arg);
+ arg = NULL;
+ }
+
+ if (!cfg->cmd_data.query.cfg_component) {
+ printf_err("No target component class specified with --source/--filter/--sink option\n");
+ goto error;
+ }
+
+ assert(params);
+ BT_MOVE(cfg->cmd_data.query.cfg_component->params, params);
+
+ /* Check for option parsing error */
+ if (opt < -1) {
+ printf_err("While parsing command-line options, at option %s: %s\n",
+ poptBadOption(pc, 0), poptStrerror(opt));
+ goto error;
+ }
+
+ /*
+ * We need exactly one leftover argument which is the
+ * mandatory object.
+ */
+ leftover = poptGetArg(pc);
+ if (leftover) {
+ if (strlen(leftover) == 0) {
+ printf_err("Invalid empty object\n");
+ goto error;
+ }
+
+ g_string_assign(cfg->cmd_data.query.object, leftover);
+ } else {
+ print_query_usage(stdout);
+ *retcode = -1;
+ BT_PUT(cfg);
+ goto end;
+ }
+
+ leftover = poptGetArg(pc);
+ if (leftover) {
+ printf_err("Invalid argument: %s\n", leftover);
+ goto error;
+ }
+
+ if (append_home_and_system_plugin_paths(
+ cfg->cmd_data.query.plugin_paths,
+ cfg->cmd_data.query.omit_system_plugin_path,
+ cfg->cmd_data.query.omit_home_plugin_path)) {
+ printf_err("Cannot append home and system plugin paths\n");
+ goto error;
+ }
+
+ goto end;
+
+error:
+ *retcode = 1;
+ BT_PUT(cfg);
+
+end:
+ if (pc) {
+ poptFreeContext(pc);
+ }
+
+ BT_PUT(params);
+ free(arg);
+ return cfg;
+}
+