diff options
Diffstat (limited to 'tools/perf/builtin-config.c')
-rw-r--r-- | tools/perf/builtin-config.c | 137 |
1 files changed, 134 insertions, 3 deletions
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c index e4207a23b52c..8c0d93b7c2f0 100644 --- a/tools/perf/builtin-config.c +++ b/tools/perf/builtin-config.c @@ -17,7 +17,7 @@ static bool use_system_config, use_user_config; static const char * const config_usage[] = { - "perf config [<file-option>] [options]", + "perf config [<file-option>] [options] [section.name[=value] ...]", NULL }; @@ -33,6 +33,73 @@ static struct option config_options[] = { OPT_END() }; +static int set_config(struct perf_config_set *set, const char *file_name, + const char *var, const char *value) +{ + struct perf_config_section *section = NULL; + struct perf_config_item *item = NULL; + const char *first_line = "# this file is auto-generated."; + FILE *fp; + + if (set == NULL) + return -1; + + fp = fopen(file_name, "w"); + if (!fp) + return -1; + + perf_config_set__collect(set, file_name, var, value); + fprintf(fp, "%s\n", first_line); + + /* overwrite configvariables */ + perf_config_items__for_each_entry(&set->sections, section) { + if (!use_system_config && section->from_system_config) + continue; + fprintf(fp, "[%s]\n", section->name); + + perf_config_items__for_each_entry(§ion->items, item) { + if (!use_system_config && section->from_system_config) + continue; + if (item->value) + fprintf(fp, "\t%s = %s\n", + item->name, item->value); + } + } + fclose(fp); + + return 0; +} + +static int show_spec_config(struct perf_config_set *set, const char *var) +{ + struct perf_config_section *section; + struct perf_config_item *item; + + if (set == NULL) + return -1; + + perf_config_items__for_each_entry(&set->sections, section) { + if (prefixcmp(var, section->name) != 0) + continue; + + perf_config_items__for_each_entry(§ion->items, item) { + const char *name = var + strlen(section->name) + 1; + + if (strcmp(name, item->name) == 0) { + char *value = item->value; + + if (value) { + printf("%s=%s\n", var, value); + return 0; + } + } + + } + } + + return 0; +} + static int show_config(struct perf_config_set *set) { struct perf_config_section *section; @@ -52,9 +119,44 @@ static int show_config(struct perf_config_set *set) return 0; } +static int parse_config_arg(char *arg, char **var, char **value) +{ + const char *last_dot = strchr(arg, '.'); + + /* + * Since "var" actually contains the section name and the real + * config variable name separated by a dot, we have to know where the dot is. + */ + if (last_dot == NULL || last_dot == arg) { + pr_err("The config variable does not contain a section name: %s\n", arg); + return -1; + } + if (!last_dot[1]) { + pr_err("The config variable does not contain a variable name: %s\n", arg); + return -1; + } + + *value = strchr(arg, '='); + if (*value == NULL) + *var = arg; + else if (!strcmp(*value, "=")) { + pr_err("The config variable does not contain a value: %s\n", arg); + return -1; + } else { + *value = *value + 1; /* excluding a first character '=' */ + *var = strsep(&arg, "="); + if (*var[0] == '\0') { + pr_err("invalid config variable: %s\n", arg); + return -1; + } + } + + return 0; +} + int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) { - int ret = 0; + int i, ret = 0; struct perf_config_set *set; char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); @@ -100,7 +202,36 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) } break; default: - usage_with_options(config_usage, config_options); + if (argc) { + for (i = 0; argv[i]; i++) { + char *var, *value; + char *arg = strdup(argv[i]); + + if (!arg) { + pr_err("%s: strdup failed\n", __func__); + ret = -1; + break; + } + + if (parse_config_arg(arg, &var, &value) < 0) { + free(arg); + ret = -1; + break; + } + + if (value == NULL) + ret = show_spec_config(set, var); + else { + const char *config_filename = config_exclusive_filename; + + if (!config_exclusive_filename) + config_filename = user_config; + ret = set_config(set, config_filename, var, value); + } + free(arg); + } + } else + usage_with_options(config_usage, config_options); } perf_config_set__delete(set); |