blob: abfa49eaf7fd1039ead9a753a0d8bb65d9dd0a90 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Taeung Song30862f22015-11-17 22:53:21 +09002/*
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"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060013#include <subcmd/parse-options.h>
Taeung Song30862f22015-11-17 22:53:21 +090014#include "util/util.h"
15#include "util/debug.h"
Taeung Song860b8d42016-04-14 16:53:19 +090016#include "util/config.h"
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030017#include <linux/string.h>
Taeung Song30862f22015-11-17 22:53:21 +090018
Taeung Songc7ac2412016-02-11 02:51:17 +090019static bool use_system_config, use_user_config;
20
Taeung Song30862f22015-11-17 22:53:21 +090021static const char * const config_usage[] = {
Taeung Songc6fc0182016-11-04 15:44:20 +090022 "perf config [<file-option>] [options] [section.name[=value] ...]",
Taeung Song30862f22015-11-17 22:53:21 +090023 NULL
24};
25
26enum actions {
27 ACTION_LIST = 1
28} actions;
29
30static struct option config_options[] = {
31 OPT_SET_UINT('l', "list", &actions,
32 "show current config variables", ACTION_LIST),
Taeung Songc7ac2412016-02-11 02:51:17 +090033 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
34 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
Taeung Song30862f22015-11-17 22:53:21 +090035 OPT_END()
36};
37
Taeung Songc6fc0182016-11-04 15:44:20 +090038static int set_config(struct perf_config_set *set, const char *file_name,
39 const char *var, const char *value)
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
Taeung Song08d090c2016-11-04 15:44:22 +090053 perf_config_set__collect(set, file_name, var, value);
Taeung Songc6fc0182016-11-04 15:44:20 +090054 fprintf(fp, "%s\n", first_line);
55
56 /* overwrite configvariables */
57 perf_config_items__for_each_entry(&set->sections, section) {
Taeung Song08d090c2016-11-04 15:44:22 +090058 if (!use_system_config && section->from_system_config)
59 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090060 fprintf(fp, "[%s]\n", section->name);
61
62 perf_config_items__for_each_entry(&section->items, item) {
Taeung Songcba225d2017-09-07 12:18:45 +090063 if (!use_system_config && item->from_system_config)
Taeung Song08d090c2016-11-04 15:44:22 +090064 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090065 if (item->value)
66 fprintf(fp, "\t%s = %s\n",
67 item->name, item->value);
68 }
69 }
70 fclose(fp);
71
72 return 0;
73}
74
Taeung Song90923602016-11-04 15:44:17 +090075static int show_spec_config(struct perf_config_set *set, const char *var)
76{
77 struct perf_config_section *section;
78 struct perf_config_item *item;
79
80 if (set == NULL)
81 return -1;
82
83 perf_config_items__for_each_entry(&set->sections, section) {
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030084 if (!strstarts(var, section->name))
Taeung Song90923602016-11-04 15:44:17 +090085 continue;
86
87 perf_config_items__for_each_entry(&section->items, item) {
88 const char *name = var + strlen(section->name) + 1;
89
90 if (strcmp(name, item->name) == 0) {
91 char *value = item->value;
92
93 if (value) {
94 printf("%s=%s\n", var, value);
95 return 0;
96 }
97 }
98
99 }
100 }
101
102 return 0;
103}
104
Taeung Song860b8d42016-04-14 16:53:19 +0900105static int show_config(struct perf_config_set *set)
Taeung Song30862f22015-11-17 22:53:21 +0900106{
Taeung Song860b8d42016-04-14 16:53:19 +0900107 struct perf_config_section *section;
108 struct perf_config_item *item;
Taeung Song860b8d42016-04-14 16:53:19 +0900109
110 if (set == NULL)
111 return -1;
112
Taeung Song4a35b342016-06-23 23:14:32 +0900113 perf_config_set__for_each_entry(set, section, item) {
114 char *value = item->value;
Taeung Song860b8d42016-04-14 16:53:19 +0900115
Taeung Song4a35b342016-06-23 23:14:32 +0900116 if (value)
117 printf("%s.%s=%s\n", section->name,
118 item->name, value);
Taeung Song860b8d42016-04-14 16:53:19 +0900119 }
Taeung Song30862f22015-11-17 22:53:21 +0900120
121 return 0;
122}
123
Taeung Songc6fc0182016-11-04 15:44:20 +0900124static int parse_config_arg(char *arg, char **var, char **value)
Taeung Song36662792016-11-04 15:44:19 +0900125{
126 const char *last_dot = strchr(arg, '.');
127
128 /*
129 * Since "var" actually contains the section name and the real
130 * config variable name separated by a dot, we have to know where the dot is.
131 */
132 if (last_dot == NULL || last_dot == arg) {
133 pr_err("The config variable does not contain a section name: %s\n", arg);
134 return -1;
135 }
136 if (!last_dot[1]) {
137 pr_err("The config variable does not contain a variable name: %s\n", arg);
138 return -1;
139 }
140
Taeung Songc6fc0182016-11-04 15:44:20 +0900141 *value = strchr(arg, '=');
142 if (*value == NULL)
143 *var = arg;
144 else if (!strcmp(*value, "=")) {
145 pr_err("The config variable does not contain a value: %s\n", arg);
146 return -1;
147 } else {
148 *value = *value + 1; /* excluding a first character '=' */
149 *var = strsep(&arg, "=");
150 if (*var[0] == '\0') {
151 pr_err("invalid config variable: %s\n", arg);
152 return -1;
153 }
154 }
155
Taeung Song36662792016-11-04 15:44:19 +0900156 return 0;
157}
158
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300159int cmd_config(int argc, const char **argv)
Taeung Song30862f22015-11-17 22:53:21 +0900160{
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900161 int i, ret = -1;
Taeung Song860b8d42016-04-14 16:53:19 +0900162 struct perf_config_set *set;
Taeung Songc7ac2412016-02-11 02:51:17 +0900163 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
Taeung Song4341ec62017-04-26 21:21:02 +0900164 const char *config_filename;
Taeung Song30862f22015-11-17 22:53:21 +0900165
166 argc = parse_options(argc, argv, config_options, config_usage,
167 PARSE_OPT_STOP_AT_NON_OPTION);
168
Taeung Songc7ac2412016-02-11 02:51:17 +0900169 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
Taeung Song4341ec62017-04-26 21:21:02 +0900181 if (!config_exclusive_filename)
182 config_filename = user_config;
183 else
184 config_filename = config_exclusive_filename;
185
Taeung Song8a0a9c72016-06-23 23:14:31 +0900186 /*
187 * At only 'config' sub-command, individually use the config set
188 * because of reinitializing with options config file location.
189 */
Taeung Song860b8d42016-04-14 16:53:19 +0900190 set = perf_config_set__new();
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900191 if (!set)
Taeung Song860b8d42016-04-14 16:53:19 +0900192 goto out_err;
Taeung Song860b8d42016-04-14 16:53:19 +0900193
Taeung Song30862f22015-11-17 22:53:21 +0900194 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 {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900200 if (show_config(set) < 0) {
Taeung Song30862f22015-11-17 22:53:21 +0900201 pr_err("Nothing configured, "
Taeung Songc7ac2412016-02-11 02:51:17 +0900202 "please check your %s \n", config_filename);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900203 goto out_err;
204 }
Taeung Song30862f22015-11-17 22:53:21 +0900205 }
206 break;
207 default:
Taeung Song8c1cedb2017-05-08 20:07:30 +0900208 if (!argc) {
Taeung Song90923602016-11-04 15:44:17 +0900209 usage_with_options(config_usage, config_options);
Taeung Song8c1cedb2017-05-08 20:07:30 +0900210 break;
211 }
212
213 for (i = 0; argv[i]; i++) {
214 char *var, *value;
215 char *arg = strdup(argv[i]);
216
217 if (!arg) {
218 pr_err("%s: strdup failed\n", __func__);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900219 goto out_err;
Taeung Song8c1cedb2017-05-08 20:07:30 +0900220 }
221
222 if (parse_config_arg(arg, &var, &value) < 0) {
223 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900224 goto out_err;
Taeung Song8c1cedb2017-05-08 20:07:30 +0900225 }
226
Taeung Song4f1fd742017-06-17 12:46:37 +0900227 if (value == NULL) {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900228 if (show_spec_config(set, var) < 0) {
Taeung Song4f1fd742017-06-17 12:46:37 +0900229 pr_err("%s is not configured: %s\n",
230 var, config_filename);
231 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900232 goto out_err;
Taeung Song4f1fd742017-06-17 12:46:37 +0900233 }
234 } else {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900235 if (set_config(set, config_filename, var, value) < 0) {
Taeung Song4f1fd742017-06-17 12:46:37 +0900236 pr_err("Failed to set '%s=%s' on %s\n",
237 var, value, config_filename);
238 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900239 goto out_err;
Taeung Song4f1fd742017-06-17 12:46:37 +0900240 }
241 }
Taeung Song8c1cedb2017-05-08 20:07:30 +0900242 free(arg);
243 }
Taeung Song30862f22015-11-17 22:53:21 +0900244 }
245
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900246 ret = 0;
Taeung Song860b8d42016-04-14 16:53:19 +0900247out_err:
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900248 perf_config_set__delete(set);
Taeung Song30862f22015-11-17 22:53:21 +0900249 return ret;
250}