blob: a1d82e33282c806e2763e4e6b6368f3ed6290f74 [file] [log] [blame]
Taeung Song30862f22015-11-17 22:53:21 +09001/*
2 * builtin-config.c
3 *
4 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
5 *
6 */
7#include "builtin.h"
8
9#include "perf.h"
10
11#include "util/cache.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Taeung Song30862f22015-11-17 22:53:21 +090013#include "util/util.h"
14#include "util/debug.h"
Taeung Song860b8d42016-04-14 16:53:19 +090015#include "util/config.h"
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030016#include <linux/string.h>
Taeung Song30862f22015-11-17 22:53:21 +090017
Taeung Songc7ac2412016-02-11 02:51:17 +090018static bool use_system_config, use_user_config;
19
Taeung Song30862f22015-11-17 22:53:21 +090020static const char * const config_usage[] = {
Taeung Songc6fc0182016-11-04 15:44:20 +090021 "perf config [<file-option>] [options] [section.name[=value] ...]",
Taeung Song30862f22015-11-17 22:53:21 +090022 NULL
23};
24
25enum actions {
26 ACTION_LIST = 1
27} actions;
28
29static struct option config_options[] = {
30 OPT_SET_UINT('l', "list", &actions,
31 "show current config variables", ACTION_LIST),
Taeung Songc7ac2412016-02-11 02:51:17 +090032 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
33 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
Taeung Song30862f22015-11-17 22:53:21 +090034 OPT_END()
35};
36
Taeung Songc6fc0182016-11-04 15:44:20 +090037static int set_config(struct perf_config_set *set, const char *file_name,
38 const char *var, const char *value)
39{
40 struct perf_config_section *section = NULL;
41 struct perf_config_item *item = NULL;
42 const char *first_line = "# this file is auto-generated.";
43 FILE *fp;
44
45 if (set == NULL)
46 return -1;
47
48 fp = fopen(file_name, "w");
49 if (!fp)
50 return -1;
51
Taeung Song08d090c2016-11-04 15:44:22 +090052 perf_config_set__collect(set, file_name, var, value);
Taeung Songc6fc0182016-11-04 15:44:20 +090053 fprintf(fp, "%s\n", first_line);
54
55 /* overwrite configvariables */
56 perf_config_items__for_each_entry(&set->sections, section) {
Taeung Song08d090c2016-11-04 15:44:22 +090057 if (!use_system_config && section->from_system_config)
58 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090059 fprintf(fp, "[%s]\n", section->name);
60
61 perf_config_items__for_each_entry(&section->items, item) {
Taeung Songcba225d2017-09-07 12:18:45 +090062 if (!use_system_config && item->from_system_config)
Taeung Song08d090c2016-11-04 15:44:22 +090063 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090064 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
Taeung Song90923602016-11-04 15:44:17 +090074static 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) {
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030083 if (!strstarts(var, section->name))
Taeung Song90923602016-11-04 15:44:17 +090084 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
Taeung Song860b8d42016-04-14 16:53:19 +0900104static int show_config(struct perf_config_set *set)
Taeung Song30862f22015-11-17 22:53:21 +0900105{
Taeung Song860b8d42016-04-14 16:53:19 +0900106 struct perf_config_section *section;
107 struct perf_config_item *item;
Taeung Song860b8d42016-04-14 16:53:19 +0900108
109 if (set == NULL)
110 return -1;
111
Taeung Song4a35b342016-06-23 23:14:32 +0900112 perf_config_set__for_each_entry(set, section, item) {
113 char *value = item->value;
Taeung Song860b8d42016-04-14 16:53:19 +0900114
Taeung Song4a35b342016-06-23 23:14:32 +0900115 if (value)
116 printf("%s.%s=%s\n", section->name,
117 item->name, value);
Taeung Song860b8d42016-04-14 16:53:19 +0900118 }
Taeung Song30862f22015-11-17 22:53:21 +0900119
120 return 0;
121}
122
Taeung Songc6fc0182016-11-04 15:44:20 +0900123static int parse_config_arg(char *arg, char **var, char **value)
Taeung Song36662792016-11-04 15:44:19 +0900124{
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
Taeung Songc6fc0182016-11-04 15:44:20 +0900140 *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
Taeung Song36662792016-11-04 15:44:19 +0900155 return 0;
156}
157
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300158int cmd_config(int argc, const char **argv)
Taeung Song30862f22015-11-17 22:53:21 +0900159{
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900160 int i, ret = -1;
Taeung Song860b8d42016-04-14 16:53:19 +0900161 struct perf_config_set *set;
Taeung Songc7ac2412016-02-11 02:51:17 +0900162 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
Taeung Song4341ec62017-04-26 21:21:02 +0900163 const char *config_filename;
Taeung Song30862f22015-11-17 22:53:21 +0900164
165 argc = parse_options(argc, argv, config_options, config_usage,
166 PARSE_OPT_STOP_AT_NON_OPTION);
167
Taeung Songc7ac2412016-02-11 02:51:17 +0900168 if (use_system_config && use_user_config) {
169 pr_err("Error: only one config file at a time\n");
170 parse_options_usage(config_usage, config_options, "user", 0);
171 parse_options_usage(NULL, config_options, "system", 0);
172 return -1;
173 }
174
175 if (use_system_config)
176 config_exclusive_filename = perf_etc_perfconfig();
177 else if (use_user_config)
178 config_exclusive_filename = user_config;
179
Taeung Song4341ec62017-04-26 21:21:02 +0900180 if (!config_exclusive_filename)
181 config_filename = user_config;
182 else
183 config_filename = config_exclusive_filename;
184
Taeung Song8a0a9c72016-06-23 23:14:31 +0900185 /*
186 * At only 'config' sub-command, individually use the config set
187 * because of reinitializing with options config file location.
188 */
Taeung Song860b8d42016-04-14 16:53:19 +0900189 set = perf_config_set__new();
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900190 if (!set)
Taeung Song860b8d42016-04-14 16:53:19 +0900191 goto out_err;
Taeung Song860b8d42016-04-14 16:53:19 +0900192
Taeung Song30862f22015-11-17 22:53:21 +0900193 switch (actions) {
194 case ACTION_LIST:
195 if (argc) {
196 pr_err("Error: takes no arguments\n");
197 parse_options_usage(config_usage, config_options, "l", 1);
198 } else {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900199 if (show_config(set) < 0) {
Taeung Song30862f22015-11-17 22:53:21 +0900200 pr_err("Nothing configured, "
Taeung Songc7ac2412016-02-11 02:51:17 +0900201 "please check your %s \n", config_filename);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900202 goto out_err;
203 }
Taeung Song30862f22015-11-17 22:53:21 +0900204 }
205 break;
206 default:
Taeung Song8c1cedb2017-05-08 20:07:30 +0900207 if (!argc) {
Taeung Song90923602016-11-04 15:44:17 +0900208 usage_with_options(config_usage, config_options);
Taeung Song8c1cedb2017-05-08 20:07:30 +0900209 break;
210 }
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__);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900218 goto out_err;
Taeung Song8c1cedb2017-05-08 20:07:30 +0900219 }
220
221 if (parse_config_arg(arg, &var, &value) < 0) {
222 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900223 goto out_err;
Taeung Song8c1cedb2017-05-08 20:07:30 +0900224 }
225
Taeung Song4f1fd742017-06-17 12:46:37 +0900226 if (value == NULL) {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900227 if (show_spec_config(set, var) < 0) {
Taeung Song4f1fd742017-06-17 12:46:37 +0900228 pr_err("%s is not configured: %s\n",
229 var, config_filename);
230 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900231 goto out_err;
Taeung Song4f1fd742017-06-17 12:46:37 +0900232 }
233 } else {
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900234 if (set_config(set, config_filename, var, value) < 0) {
Taeung Song4f1fd742017-06-17 12:46:37 +0900235 pr_err("Failed to set '%s=%s' on %s\n",
236 var, value, config_filename);
237 free(arg);
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900238 goto out_err;
Taeung Song4f1fd742017-06-17 12:46:37 +0900239 }
240 }
Taeung Song8c1cedb2017-05-08 20:07:30 +0900241 free(arg);
242 }
Taeung Song30862f22015-11-17 22:53:21 +0900243 }
244
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900245 ret = 0;
Taeung Song860b8d42016-04-14 16:53:19 +0900246out_err:
Taeung Songdfe1c6d2017-06-17 12:46:42 +0900247 perf_config_set__delete(set);
Taeung Song30862f22015-11-17 22:53:21 +0900248 return ret;
249}