linux/tools/perf/builtin-config.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * builtin-config.c
   4 *
   5 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
   6 *
   7 */
   8#include "builtin.h"
   9
  10#include "perf.h"
  11
  12#include "util/cache.h"
  13#include <subcmd/parse-options.h>
  14#include "util/util.h"
  15#include "util/debug.h"
  16#include "util/config.h"
  17#include <linux/string.h>
  18#include <stdlib.h>
  19
  20static bool use_system_config, use_user_config;
  21
  22static const char * const config_usage[] = {
  23        "perf config [<file-option>] [options] [section.name[=value] ...]",
  24        NULL
  25};
  26
  27enum actions {
  28        ACTION_LIST = 1
  29} actions;
  30
  31static struct option config_options[] = {
  32        OPT_SET_UINT('l', "list", &actions,
  33                     "show current config variables", ACTION_LIST),
  34        OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
  35        OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
  36        OPT_END()
  37};
  38
  39static int set_config(struct perf_config_set *set, const char *file_name)
  40{
  41        struct perf_config_section *section = NULL;
  42        struct perf_config_item *item = NULL;
  43        const char *first_line = "# this file is auto-generated.";
  44        FILE *fp;
  45
  46        if (set == NULL)
  47                return -1;
  48
  49        fp = fopen(file_name, "w");
  50        if (!fp)
  51                return -1;
  52
  53        fprintf(fp, "%s\n", first_line);
  54
  55        /* overwrite configvariables */
  56        perf_config_items__for_each_entry(&set->sections, section) {
  57                if (!use_system_config && section->from_system_config)
  58                        continue;
  59                fprintf(fp, "[%s]\n", section->name);
  60
  61                perf_config_items__for_each_entry(&section->items, item) {
  62                        if (!use_system_config && item->from_system_config)
  63                                continue;
  64                        if (item->value)
  65                                fprintf(fp, "\t%s = %s\n",
  66                                        item->name, item->value);
  67                }
  68        }
  69        fclose(fp);
  70
  71        return 0;
  72}
  73
  74static int show_spec_config(struct perf_config_set *set, const char *var)
  75{
  76        struct perf_config_section *section;
  77        struct perf_config_item *item;
  78
  79        if (set == NULL)
  80                return -1;
  81
  82        perf_config_items__for_each_entry(&set->sections, section) {
  83                if (!strstarts(var, section->name))
  84                        continue;
  85
  86                perf_config_items__for_each_entry(&section->items, item) {
  87                        const char *name = var + strlen(section->name) + 1;
  88
  89                        if (strcmp(name, item->name) == 0) {
  90                                char *value = item->value;
  91
  92                                if (value) {
  93                                        printf("%s=%s\n", var, value);
  94                                        return 0;
  95                                }
  96                        }
  97
  98                }
  99        }
 100
 101        return 0;
 102}
 103
 104static int show_config(struct perf_config_set *set)
 105{
 106        struct perf_config_section *section;
 107        struct perf_config_item *item;
 108
 109        if (set == NULL)
 110                return -1;
 111
 112        perf_config_set__for_each_entry(set, section, item) {
 113                char *value = item->value;
 114
 115                if (value)
 116                        printf("%s.%s=%s\n", section->name,
 117                               item->name, value);
 118        }
 119
 120        return 0;
 121}
 122
 123static int parse_config_arg(char *arg, char **var, char **value)
 124{
 125        const char *last_dot = strchr(arg, '.');
 126
 127        /*
 128         * Since "var" actually contains the section name and the real
 129         * config variable name separated by a dot, we have to know where the dot is.
 130         */
 131        if (last_dot == NULL || last_dot == arg) {
 132                pr_err("The config variable does not contain a section name: %s\n", arg);
 133                return -1;
 134        }
 135        if (!last_dot[1]) {
 136                pr_err("The config variable does not contain a variable name: %s\n", arg);
 137                return -1;
 138        }
 139
 140        *value = strchr(arg, '=');
 141        if (*value == NULL)
 142                *var = arg;
 143        else if (!strcmp(*value, "=")) {
 144                pr_err("The config variable does not contain a value: %s\n", arg);
 145                return -1;
 146        } else {
 147                *value = *value + 1; /* excluding a first character '=' */
 148                *var = strsep(&arg, "=");
 149                if (*var[0] == '\0') {
 150                        pr_err("invalid config variable: %s\n", arg);
 151                        return -1;
 152                }
 153        }
 154
 155        return 0;
 156}
 157
 158int cmd_config(int argc, const char **argv)
 159{
 160        int i, ret = -1;
 161        struct perf_config_set *set;
 162        char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
 163        const char *config_filename;
 164        bool changed = false;
 165
 166        argc = parse_options(argc, argv, config_options, config_usage,
 167                             PARSE_OPT_STOP_AT_NON_OPTION);
 168
 169        if (use_system_config && use_user_config) {
 170                pr_err("Error: only one config file at a time\n");
 171                parse_options_usage(config_usage, config_options, "user", 0);
 172                parse_options_usage(NULL, config_options, "system", 0);
 173                return -1;
 174        }
 175
 176        if (use_system_config)
 177                config_exclusive_filename = perf_etc_perfconfig();
 178        else if (use_user_config)
 179                config_exclusive_filename = user_config;
 180
 181        if (!config_exclusive_filename)
 182                config_filename = user_config;
 183        else
 184                config_filename = config_exclusive_filename;
 185
 186        /*
 187         * At only 'config' sub-command, individually use the config set
 188         * because of reinitializing with options config file location.
 189         */
 190        set = perf_config_set__new();
 191        if (!set)
 192                goto out_err;
 193
 194        switch (actions) {
 195        case ACTION_LIST:
 196                if (argc) {
 197                        pr_err("Error: takes no arguments\n");
 198                        parse_options_usage(config_usage, config_options, "l", 1);
 199                } else {
 200do_action_list:
 201                        if (show_config(set) < 0) {
 202                                pr_err("Nothing configured, "
 203                                       "please check your %s \n", config_filename);
 204                                goto out_err;
 205                        }
 206                }
 207                break;
 208        default:
 209                if (!argc)
 210                        goto do_action_list;
 211
 212                for (i = 0; argv[i]; i++) {
 213                        char *var, *value;
 214                        char *arg = strdup(argv[i]);
 215
 216                        if (!arg) {
 217                                pr_err("%s: strdup failed\n", __func__);
 218                                goto out_err;
 219                        }
 220
 221                        if (parse_config_arg(arg, &var, &value) < 0) {
 222                                free(arg);
 223                                goto out_err;
 224                        }
 225
 226                        if (value == NULL) {
 227                                if (show_spec_config(set, var) < 0) {
 228                                        pr_err("%s is not configured: %s\n",
 229                                               var, config_filename);
 230                                        free(arg);
 231                                        goto out_err;
 232                                }
 233                        } else {
 234                                if (perf_config_set__collect(set, config_filename,
 235                                                             var, value) < 0) {
 236                                        pr_err("Failed to add '%s=%s'\n",
 237                                               var, value);
 238                                        free(arg);
 239                                        goto out_err;
 240                                }
 241                                changed = true;
 242                        }
 243                        free(arg);
 244                }
 245
 246                if (!changed)
 247                        break;
 248
 249                if (set_config(set, config_filename) < 0) {
 250                        pr_err("Failed to set the configs on %s\n",
 251                               config_filename);
 252                        goto out_err;
 253                }
 254        }
 255
 256        ret = 0;
 257out_err:
 258        perf_config_set__delete(set);
 259        return ret;
 260}
 261