Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
Namhyung Kim | 5f9273d | 2011-12-13 22:52:03 +0900 | [diff] [blame] | 2 | * config.c |
| 3 | * |
| 4 | * Helper functions for parsing config items. |
| 5 | * Originally copied from GIT source. |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) Linus Torvalds, 2005 |
| 8 | * Copyright (C) Johannes Schindelin, 2005 |
| 9 | * |
| 10 | */ |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 11 | #include <errno.h> |
Arnaldo Carvalho de Melo | 391e420 | 2017-04-19 18:51:14 -0300 | [diff] [blame] | 12 | #include <sys/param.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 13 | #include "util.h" |
| 14 | #include "cache.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 15 | #include <subcmd/exec-cmd.h> |
Namhyung Kim | 0b93da1 | 2014-01-14 12:02:15 +0900 | [diff] [blame] | 16 | #include "util/hist.h" /* perf_hist_config */ |
Wang Nan | aa61fd0 | 2015-07-21 11:13:34 +0000 | [diff] [blame] | 17 | #include "util/llvm-utils.h" /* perf_llvm_config */ |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 18 | #include "config.h" |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 22 | #include <linux/string.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 24 | #include "sane_ctype.h" |
| 25 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 26 | #define MAXNAME (256) |
| 27 | |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 28 | #define DEBUG_CACHE_DIR ".debug" |
| 29 | |
| 30 | |
| 31 | char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ |
| 32 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 33 | static FILE *config_file; |
| 34 | static const char *config_file_name; |
| 35 | static int config_linenr; |
| 36 | static int config_file_eof; |
Taeung Song | 8a0a9c7 | 2016-06-23 23:14:31 +0900 | [diff] [blame] | 37 | static struct perf_config_set *config_set; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 38 | |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 39 | const char *config_exclusive_filename; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 40 | |
| 41 | static int get_next_char(void) |
| 42 | { |
| 43 | int c; |
| 44 | FILE *f; |
| 45 | |
| 46 | c = '\n'; |
| 47 | if ((f = config_file) != NULL) { |
| 48 | c = fgetc(f); |
| 49 | if (c == '\r') { |
| 50 | /* DOS like systems */ |
| 51 | c = fgetc(f); |
| 52 | if (c != '\n') { |
| 53 | ungetc(c, f); |
| 54 | c = '\r'; |
| 55 | } |
| 56 | } |
| 57 | if (c == '\n') |
| 58 | config_linenr++; |
| 59 | if (c == EOF) { |
| 60 | config_file_eof = 1; |
| 61 | c = '\n'; |
| 62 | } |
| 63 | } |
| 64 | return c; |
| 65 | } |
| 66 | |
| 67 | static char *parse_value(void) |
| 68 | { |
| 69 | static char value[1024]; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 70 | int quote = 0, comment = 0, space = 0; |
| 71 | size_t len = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 72 | |
| 73 | for (;;) { |
| 74 | int c = get_next_char(); |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 75 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 76 | if (len >= sizeof(value) - 1) |
| 77 | return NULL; |
| 78 | if (c == '\n') { |
| 79 | if (quote) |
| 80 | return NULL; |
| 81 | value[len] = 0; |
| 82 | return value; |
| 83 | } |
| 84 | if (comment) |
| 85 | continue; |
| 86 | if (isspace(c) && !quote) { |
| 87 | space = 1; |
| 88 | continue; |
| 89 | } |
| 90 | if (!quote) { |
| 91 | if (c == ';' || c == '#') { |
| 92 | comment = 1; |
| 93 | continue; |
| 94 | } |
| 95 | } |
| 96 | if (space) { |
| 97 | if (len) |
| 98 | value[len++] = ' '; |
| 99 | space = 0; |
| 100 | } |
| 101 | if (c == '\\') { |
| 102 | c = get_next_char(); |
| 103 | switch (c) { |
| 104 | case '\n': |
| 105 | continue; |
| 106 | case 't': |
| 107 | c = '\t'; |
| 108 | break; |
| 109 | case 'b': |
| 110 | c = '\b'; |
| 111 | break; |
| 112 | case 'n': |
| 113 | c = '\n'; |
| 114 | break; |
| 115 | /* Some characters escape as themselves */ |
| 116 | case '\\': case '"': |
| 117 | break; |
| 118 | /* Reject unknown escape sequences */ |
| 119 | default: |
| 120 | return NULL; |
| 121 | } |
| 122 | value[len++] = c; |
| 123 | continue; |
| 124 | } |
| 125 | if (c == '"') { |
| 126 | quote = 1-quote; |
| 127 | continue; |
| 128 | } |
| 129 | value[len++] = c; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static inline int iskeychar(int c) |
| 134 | { |
Arnaldo Carvalho de Melo | 8dc7c65 | 2012-05-29 21:59:02 -0300 | [diff] [blame] | 135 | return isalnum(c) || c == '-' || c == '_'; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) |
| 139 | { |
| 140 | int c; |
| 141 | char *value; |
| 142 | |
| 143 | /* Get the full name */ |
| 144 | for (;;) { |
| 145 | c = get_next_char(); |
| 146 | if (config_file_eof) |
| 147 | break; |
| 148 | if (!iskeychar(c)) |
| 149 | break; |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 150 | name[len++] = c; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 151 | if (len >= MAXNAME) |
| 152 | return -1; |
| 153 | } |
| 154 | name[len] = 0; |
| 155 | while (c == ' ' || c == '\t') |
| 156 | c = get_next_char(); |
| 157 | |
| 158 | value = NULL; |
| 159 | if (c != '\n') { |
| 160 | if (c != '=') |
| 161 | return -1; |
| 162 | value = parse_value(); |
| 163 | if (!value) |
| 164 | return -1; |
| 165 | } |
| 166 | return fn(name, value, data); |
| 167 | } |
| 168 | |
| 169 | static int get_extended_base_var(char *name, int baselen, int c) |
| 170 | { |
| 171 | do { |
| 172 | if (c == '\n') |
| 173 | return -1; |
| 174 | c = get_next_char(); |
| 175 | } while (isspace(c)); |
| 176 | |
| 177 | /* We require the format to be '[base "extension"]' */ |
| 178 | if (c != '"') |
| 179 | return -1; |
| 180 | name[baselen++] = '.'; |
| 181 | |
| 182 | for (;;) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 183 | int ch = get_next_char(); |
| 184 | |
| 185 | if (ch == '\n') |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 186 | return -1; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 187 | if (ch == '"') |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 188 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 189 | if (ch == '\\') { |
| 190 | ch = get_next_char(); |
| 191 | if (ch == '\n') |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 192 | return -1; |
| 193 | } |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 194 | name[baselen++] = ch; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 195 | if (baselen > MAXNAME / 2) |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | /* Final ']' */ |
| 200 | if (get_next_char() != ']') |
| 201 | return -1; |
| 202 | return baselen; |
| 203 | } |
| 204 | |
| 205 | static int get_base_var(char *name) |
| 206 | { |
| 207 | int baselen = 0; |
| 208 | |
| 209 | for (;;) { |
| 210 | int c = get_next_char(); |
| 211 | if (config_file_eof) |
| 212 | return -1; |
| 213 | if (c == ']') |
| 214 | return baselen; |
| 215 | if (isspace(c)) |
| 216 | return get_extended_base_var(name, baselen, c); |
| 217 | if (!iskeychar(c) && c != '.') |
| 218 | return -1; |
| 219 | if (baselen > MAXNAME / 2) |
| 220 | return -1; |
| 221 | name[baselen++] = tolower(c); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static int perf_parse_file(config_fn_t fn, void *data) |
| 226 | { |
| 227 | int comment = 0; |
| 228 | int baselen = 0; |
| 229 | static char var[MAXNAME]; |
| 230 | |
| 231 | /* U+FEFF Byte Order Mark in UTF8 */ |
| 232 | static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; |
| 233 | const unsigned char *bomptr = utf8_bom; |
| 234 | |
| 235 | for (;;) { |
Jiri Olsa | 49757c9 | 2014-09-23 13:56:56 +0200 | [diff] [blame] | 236 | int line, c = get_next_char(); |
| 237 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 238 | if (bomptr && *bomptr) { |
| 239 | /* We are at the file beginning; skip UTF8-encoded BOM |
| 240 | * if present. Sane editors won't put this in on their |
| 241 | * own, but e.g. Windows Notepad will do it happily. */ |
| 242 | if ((unsigned char) c == *bomptr) { |
| 243 | bomptr++; |
| 244 | continue; |
| 245 | } else { |
| 246 | /* Do not tolerate partial BOM. */ |
| 247 | if (bomptr != utf8_bom) |
| 248 | break; |
| 249 | /* No BOM at file beginning. Cool. */ |
| 250 | bomptr = NULL; |
| 251 | } |
| 252 | } |
| 253 | if (c == '\n') { |
| 254 | if (config_file_eof) |
| 255 | return 0; |
| 256 | comment = 0; |
| 257 | continue; |
| 258 | } |
| 259 | if (comment || isspace(c)) |
| 260 | continue; |
| 261 | if (c == '#' || c == ';') { |
| 262 | comment = 1; |
| 263 | continue; |
| 264 | } |
| 265 | if (c == '[') { |
| 266 | baselen = get_base_var(var); |
| 267 | if (baselen <= 0) |
| 268 | break; |
| 269 | var[baselen++] = '.'; |
| 270 | var[baselen] = 0; |
| 271 | continue; |
| 272 | } |
| 273 | if (!isalpha(c)) |
| 274 | break; |
| 275 | var[baselen] = tolower(c); |
Jiri Olsa | 49757c9 | 2014-09-23 13:56:56 +0200 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * The get_value function might or might not reach the '\n', |
| 279 | * so saving the current line number for error reporting. |
| 280 | */ |
| 281 | line = config_linenr; |
| 282 | if (get_value(fn, data, var, baselen+1) < 0) { |
| 283 | config_linenr = line; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 284 | break; |
Jiri Olsa | 49757c9 | 2014-09-23 13:56:56 +0200 | [diff] [blame] | 285 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 286 | } |
Taeung Song | 78f71c9 | 2016-06-06 19:52:52 +0900 | [diff] [blame] | 287 | pr_err("bad config file line %d in %s\n", config_linenr, config_file_name); |
| 288 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int parse_unit_factor(const char *end, unsigned long *val) |
| 292 | { |
| 293 | if (!*end) |
| 294 | return 1; |
| 295 | else if (!strcasecmp(end, "k")) { |
| 296 | *val *= 1024; |
| 297 | return 1; |
| 298 | } |
| 299 | else if (!strcasecmp(end, "m")) { |
| 300 | *val *= 1024 * 1024; |
| 301 | return 1; |
| 302 | } |
| 303 | else if (!strcasecmp(end, "g")) { |
| 304 | *val *= 1024 * 1024 * 1024; |
| 305 | return 1; |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Jiri Olsa | 94c0655 | 2014-06-06 05:27:28 -0400 | [diff] [blame] | 310 | static int perf_parse_llong(const char *value, long long *ret) |
| 311 | { |
| 312 | if (value && *value) { |
| 313 | char *end; |
| 314 | long long val = strtoll(value, &end, 0); |
| 315 | unsigned long factor = 1; |
| 316 | |
| 317 | if (!parse_unit_factor(end, &factor)) |
| 318 | return 0; |
| 319 | *ret = val * factor; |
| 320 | return 1; |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 325 | static int perf_parse_long(const char *value, long *ret) |
| 326 | { |
| 327 | if (value && *value) { |
| 328 | char *end; |
| 329 | long val = strtol(value, &end, 0); |
| 330 | unsigned long factor = 1; |
| 331 | if (!parse_unit_factor(end, &factor)) |
| 332 | return 0; |
| 333 | *ret = val * factor; |
| 334 | return 1; |
| 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 339 | static void bad_config(const char *name) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 340 | { |
| 341 | if (config_file_name) |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 342 | pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name); |
| 343 | else |
| 344 | pr_warning("bad config value for '%s', ignoring...\n", name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 347 | int perf_config_u64(u64 *dest, const char *name, const char *value) |
Jiri Olsa | 94c0655 | 2014-06-06 05:27:28 -0400 | [diff] [blame] | 348 | { |
| 349 | long long ret = 0; |
| 350 | |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 351 | if (!perf_parse_llong(value, &ret)) { |
| 352 | bad_config(name); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | *dest = ret; |
| 357 | return 0; |
Jiri Olsa | 94c0655 | 2014-06-06 05:27:28 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 360 | int perf_config_int(int *dest, const char *name, const char *value) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 361 | { |
| 362 | long ret = 0; |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 363 | if (!perf_parse_long(value, &ret)) { |
| 364 | bad_config(name); |
| 365 | return -1; |
| 366 | } |
| 367 | *dest = ret; |
| 368 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Arnaldo Carvalho de Melo | a41794c | 2010-05-18 18:29:23 -0300 | [diff] [blame] | 371 | static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 372 | { |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 373 | int ret; |
| 374 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 375 | *is_bool = 1; |
| 376 | if (!value) |
| 377 | return 1; |
| 378 | if (!*value) |
| 379 | return 0; |
| 380 | if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on")) |
| 381 | return 1; |
| 382 | if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off")) |
| 383 | return 0; |
| 384 | *is_bool = 0; |
Arnaldo Carvalho de Melo | 25ce4bb | 2017-06-27 11:44:58 -0300 | [diff] [blame] | 385 | return perf_config_int(&ret, name, value) < 0 ? -1 : ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int perf_config_bool(const char *name, const char *value) |
| 389 | { |
| 390 | int discard; |
| 391 | return !!perf_config_bool_or_int(name, value, &discard); |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 814b3f5 | 2016-06-16 17:10:46 -0300 | [diff] [blame] | 394 | static const char *perf_config_dirname(const char *name, const char *value) |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 395 | { |
| 396 | if (!name) |
| 397 | return NULL; |
| 398 | return value; |
| 399 | } |
| 400 | |
Taeung Song | 9cb5987 | 2016-03-28 02:22:19 +0900 | [diff] [blame] | 401 | static int perf_buildid_config(const char *var, const char *value) |
| 402 | { |
| 403 | /* same dir for all commands */ |
| 404 | if (!strcmp(var, "buildid.dir")) { |
Vinson Lee | d8e2865 | 2016-04-04 22:07:39 +0000 | [diff] [blame] | 405 | const char *dir = perf_config_dirname(var, value); |
Taeung Song | 9cb5987 | 2016-03-28 02:22:19 +0900 | [diff] [blame] | 406 | |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 407 | if (!dir) { |
| 408 | pr_err("Invalid buildid directory!\n"); |
Taeung Song | 9cb5987 | 2016-03-28 02:22:19 +0900 | [diff] [blame] | 409 | return -1; |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 410 | } |
Vinson Lee | d8e2865 | 2016-04-04 22:07:39 +0000 | [diff] [blame] | 411 | strncpy(buildid_dir, dir, MAXPATHLEN-1); |
Taeung Song | 9cb5987 | 2016-03-28 02:22:19 +0900 | [diff] [blame] | 412 | buildid_dir[MAXPATHLEN-1] = '\0'; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 418 | static int perf_default_core_config(const char *var __maybe_unused, |
| 419 | const char *value __maybe_unused) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 420 | { |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 421 | /* Add other config variables here. */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 425 | static int perf_ui_config(const char *var, const char *value) |
| 426 | { |
| 427 | /* Add other config variables here. */ |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 428 | if (!strcmp(var, "ui.show-headers")) |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 429 | symbol_conf.show_hist_headers = perf_config_bool(var, value); |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 430 | |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 434 | int perf_default_config(const char *var, const char *value, |
| 435 | void *dummy __maybe_unused) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 436 | { |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 437 | if (strstarts(var, "core.")) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 438 | return perf_default_core_config(var, value); |
| 439 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 440 | if (strstarts(var, "hist.")) |
Namhyung Kim | 0b93da1 | 2014-01-14 12:02:15 +0900 | [diff] [blame] | 441 | return perf_hist_config(var, value); |
| 442 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 443 | if (strstarts(var, "ui.")) |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 444 | return perf_ui_config(var, value); |
| 445 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 446 | if (strstarts(var, "call-graph.")) |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 447 | return perf_callchain_config(var, value); |
| 448 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 449 | if (strstarts(var, "llvm.")) |
Wang Nan | aa61fd0 | 2015-07-21 11:13:34 +0000 | [diff] [blame] | 450 | return perf_llvm_config(var, value); |
| 451 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame^] | 452 | if (strstarts(var, "buildid.")) |
Taeung Song | 9cb5987 | 2016-03-28 02:22:19 +0900 | [diff] [blame] | 453 | return perf_buildid_config(var, value); |
| 454 | |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 455 | /* Add other config variables here. */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Arnaldo Carvalho de Melo | a41794c | 2010-05-18 18:29:23 -0300 | [diff] [blame] | 459 | static int perf_config_from_file(config_fn_t fn, const char *filename, void *data) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 460 | { |
| 461 | int ret; |
| 462 | FILE *f = fopen(filename, "r"); |
| 463 | |
| 464 | ret = -1; |
| 465 | if (f) { |
| 466 | config_file = f; |
| 467 | config_file_name = filename; |
| 468 | config_linenr = 1; |
| 469 | config_file_eof = 0; |
| 470 | ret = perf_parse_file(fn, data); |
| 471 | fclose(f); |
| 472 | config_file_name = NULL; |
| 473 | } |
| 474 | return ret; |
| 475 | } |
| 476 | |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 477 | const char *perf_etc_perfconfig(void) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 478 | { |
| 479 | static const char *system_wide; |
| 480 | if (!system_wide) |
| 481 | system_wide = system_path(ETC_PERFCONFIG); |
| 482 | return system_wide; |
| 483 | } |
| 484 | |
| 485 | static int perf_env_bool(const char *k, int def) |
| 486 | { |
| 487 | const char *v = getenv(k); |
| 488 | return v ? perf_config_bool(k, v) : def; |
| 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | a41794c | 2010-05-18 18:29:23 -0300 | [diff] [blame] | 491 | static int perf_config_system(void) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 492 | { |
| 493 | return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); |
| 494 | } |
| 495 | |
Arnaldo Carvalho de Melo | a41794c | 2010-05-18 18:29:23 -0300 | [diff] [blame] | 496 | static int perf_config_global(void) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 497 | { |
| 498 | return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); |
| 499 | } |
| 500 | |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 501 | static struct perf_config_section *find_section(struct list_head *sections, |
| 502 | const char *section_name) |
| 503 | { |
| 504 | struct perf_config_section *section; |
| 505 | |
| 506 | list_for_each_entry(section, sections, node) |
| 507 | if (!strcmp(section->name, section_name)) |
| 508 | return section; |
| 509 | |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | static struct perf_config_item *find_config_item(const char *name, |
| 514 | struct perf_config_section *section) |
| 515 | { |
| 516 | struct perf_config_item *item; |
| 517 | |
| 518 | list_for_each_entry(item, §ion->items, node) |
| 519 | if (!strcmp(item->name, name)) |
| 520 | return item; |
| 521 | |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | static struct perf_config_section *add_section(struct list_head *sections, |
| 526 | const char *section_name) |
| 527 | { |
| 528 | struct perf_config_section *section = zalloc(sizeof(*section)); |
| 529 | |
| 530 | if (!section) |
| 531 | return NULL; |
| 532 | |
| 533 | INIT_LIST_HEAD(§ion->items); |
| 534 | section->name = strdup(section_name); |
| 535 | if (!section->name) { |
| 536 | pr_debug("%s: strdup failed\n", __func__); |
| 537 | free(section); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | list_add_tail(§ion->node, sections); |
| 542 | return section; |
| 543 | } |
| 544 | |
| 545 | static struct perf_config_item *add_config_item(struct perf_config_section *section, |
| 546 | const char *name) |
| 547 | { |
| 548 | struct perf_config_item *item = zalloc(sizeof(*item)); |
| 549 | |
| 550 | if (!item) |
| 551 | return NULL; |
| 552 | |
| 553 | item->name = strdup(name); |
| 554 | if (!item->name) { |
| 555 | pr_debug("%s: strdup failed\n", __func__); |
| 556 | free(item); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | list_add_tail(&item->node, §ion->items); |
| 561 | return item; |
| 562 | } |
| 563 | |
| 564 | static int set_value(struct perf_config_item *item, const char *value) |
| 565 | { |
| 566 | char *val = strdup(value); |
| 567 | |
| 568 | if (!val) |
| 569 | return -1; |
| 570 | |
| 571 | zfree(&item->value); |
| 572 | item->value = val; |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int collect_config(const char *var, const char *value, |
| 577 | void *perf_config_set) |
| 578 | { |
| 579 | int ret = -1; |
| 580 | char *ptr, *key; |
| 581 | char *section_name, *name; |
| 582 | struct perf_config_section *section = NULL; |
| 583 | struct perf_config_item *item = NULL; |
| 584 | struct perf_config_set *set = perf_config_set; |
Taeung Song | 7db91f2 | 2016-06-06 19:52:54 +0900 | [diff] [blame] | 585 | struct list_head *sections; |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 586 | |
Taeung Song | 7db91f2 | 2016-06-06 19:52:54 +0900 | [diff] [blame] | 587 | if (set == NULL) |
| 588 | return -1; |
| 589 | |
| 590 | sections = &set->sections; |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 591 | key = ptr = strdup(var); |
| 592 | if (!key) { |
| 593 | pr_debug("%s: strdup failed\n", __func__); |
| 594 | return -1; |
| 595 | } |
| 596 | |
| 597 | section_name = strsep(&ptr, "."); |
| 598 | name = ptr; |
| 599 | if (name == NULL || value == NULL) |
| 600 | goto out_free; |
| 601 | |
| 602 | section = find_section(sections, section_name); |
| 603 | if (!section) { |
| 604 | section = add_section(sections, section_name); |
| 605 | if (!section) |
| 606 | goto out_free; |
| 607 | } |
| 608 | |
| 609 | item = find_config_item(name, section); |
| 610 | if (!item) { |
| 611 | item = add_config_item(section, name); |
| 612 | if (!item) |
| 613 | goto out_free; |
| 614 | } |
| 615 | |
Taeung Song | 08d090c | 2016-11-04 15:44:22 +0900 | [diff] [blame] | 616 | /* perf_config_set can contain both user and system config items. |
| 617 | * So we should know where each value is from. |
| 618 | * The classification would be needed when a particular config file |
| 619 | * is overwrited by setting feature i.e. set_config(). |
| 620 | */ |
| 621 | if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) { |
| 622 | section->from_system_config = true; |
| 623 | item->from_system_config = true; |
| 624 | } else { |
| 625 | section->from_system_config = false; |
| 626 | item->from_system_config = false; |
| 627 | } |
| 628 | |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 629 | ret = set_value(item, value); |
| 630 | return ret; |
| 631 | |
| 632 | out_free: |
| 633 | free(key); |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 634 | return -1; |
| 635 | } |
| 636 | |
Taeung Song | 08d090c | 2016-11-04 15:44:22 +0900 | [diff] [blame] | 637 | int perf_config_set__collect(struct perf_config_set *set, const char *file_name, |
Taeung Song | c6fc018 | 2016-11-04 15:44:20 +0900 | [diff] [blame] | 638 | const char *var, const char *value) |
| 639 | { |
Taeung Song | 08d090c | 2016-11-04 15:44:22 +0900 | [diff] [blame] | 640 | config_file_name = file_name; |
Taeung Song | c6fc018 | 2016-11-04 15:44:20 +0900 | [diff] [blame] | 641 | return collect_config(var, value, set); |
| 642 | } |
| 643 | |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 644 | static int perf_config_set__init(struct perf_config_set *set) |
| 645 | { |
| 646 | int ret = -1; |
| 647 | const char *home = NULL; |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 648 | char *user_config; |
| 649 | struct stat st; |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 650 | |
| 651 | /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ |
| 652 | if (config_exclusive_filename) |
| 653 | return perf_config_from_file(collect_config, config_exclusive_filename, set); |
| 654 | if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) { |
| 655 | if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0) |
| 656 | goto out; |
| 657 | } |
| 658 | |
| 659 | home = getenv("HOME"); |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 660 | |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 661 | /* |
| 662 | * Skip reading user config if: |
| 663 | * - there is no place to read it from (HOME) |
| 664 | * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) |
| 665 | */ |
| 666 | if (!home || !*home || !perf_config_global()) |
| 667 | return 0; |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 668 | |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 669 | user_config = strdup(mkpath("%s/.perfconfig", home)); |
| 670 | if (user_config == NULL) { |
Arnaldo Carvalho de Melo | 4cf134e | 2017-06-27 11:03:17 -0300 | [diff] [blame] | 671 | pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home); |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 672 | goto out; |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 673 | } |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 674 | |
| 675 | if (stat(user_config, &st) < 0) { |
| 676 | if (errno == ENOENT) |
| 677 | ret = 0; |
| 678 | goto out_free; |
| 679 | } |
| 680 | |
| 681 | ret = 0; |
| 682 | |
| 683 | if (st.st_uid && (st.st_uid != geteuid())) { |
Arnaldo Carvalho de Melo | 4cf134e | 2017-06-27 11:03:17 -0300 | [diff] [blame] | 684 | pr_warning("File %s not owned by current user or root, ignoring it.", user_config); |
Jiri Olsa | 3e00cbe | 2017-03-30 16:46:37 +0200 | [diff] [blame] | 685 | goto out_free; |
| 686 | } |
| 687 | |
| 688 | if (st.st_size) |
| 689 | ret = perf_config_from_file(collect_config, user_config, set); |
| 690 | |
| 691 | out_free: |
| 692 | free(user_config); |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 693 | out: |
| 694 | return ret; |
| 695 | } |
| 696 | |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 697 | struct perf_config_set *perf_config_set__new(void) |
| 698 | { |
| 699 | struct perf_config_set *set = zalloc(sizeof(*set)); |
| 700 | |
| 701 | if (set) { |
| 702 | INIT_LIST_HEAD(&set->sections); |
Taeung Song | 8beeb00 | 2016-06-07 18:26:12 +0900 | [diff] [blame] | 703 | if (perf_config_set__init(set) < 0) { |
Taeung Song | 25d8f48 | 2016-06-07 18:26:11 +0900 | [diff] [blame] | 704 | perf_config_set__delete(set); |
| 705 | set = NULL; |
| 706 | } |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return set; |
| 710 | } |
| 711 | |
Taeung Song | 8a0a9c7 | 2016-06-23 23:14:31 +0900 | [diff] [blame] | 712 | int perf_config(config_fn_t fn, void *data) |
| 713 | { |
| 714 | int ret = 0; |
| 715 | char key[BUFSIZ]; |
| 716 | struct perf_config_section *section; |
| 717 | struct perf_config_item *item; |
| 718 | |
| 719 | if (config_set == NULL) |
| 720 | return -1; |
| 721 | |
| 722 | perf_config_set__for_each_entry(config_set, section, item) { |
| 723 | char *value = item->value; |
| 724 | |
| 725 | if (value) { |
| 726 | scnprintf(key, sizeof(key), "%s.%s", |
| 727 | section->name, item->name); |
| 728 | ret = fn(key, value, data); |
| 729 | if (ret < 0) { |
| 730 | pr_err("Error: wrong config key-value pair %s=%s\n", |
| 731 | key, value); |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | void perf_config__init(void) |
| 741 | { |
| 742 | if (config_set == NULL) |
| 743 | config_set = perf_config_set__new(); |
| 744 | } |
| 745 | |
| 746 | void perf_config__exit(void) |
| 747 | { |
| 748 | perf_config_set__delete(config_set); |
| 749 | config_set = NULL; |
| 750 | } |
| 751 | |
| 752 | void perf_config__refresh(void) |
| 753 | { |
| 754 | perf_config__exit(); |
| 755 | perf_config__init(); |
| 756 | } |
| 757 | |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 758 | static void perf_config_item__delete(struct perf_config_item *item) |
| 759 | { |
| 760 | zfree(&item->name); |
| 761 | zfree(&item->value); |
| 762 | free(item); |
| 763 | } |
| 764 | |
| 765 | static void perf_config_section__purge(struct perf_config_section *section) |
| 766 | { |
| 767 | struct perf_config_item *item, *tmp; |
| 768 | |
| 769 | list_for_each_entry_safe(item, tmp, §ion->items, node) { |
| 770 | list_del_init(&item->node); |
| 771 | perf_config_item__delete(item); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static void perf_config_section__delete(struct perf_config_section *section) |
| 776 | { |
| 777 | perf_config_section__purge(section); |
| 778 | zfree(§ion->name); |
| 779 | free(section); |
| 780 | } |
| 781 | |
| 782 | static void perf_config_set__purge(struct perf_config_set *set) |
| 783 | { |
| 784 | struct perf_config_section *section, *tmp; |
| 785 | |
| 786 | list_for_each_entry_safe(section, tmp, &set->sections, node) { |
| 787 | list_del_init(§ion->node); |
| 788 | perf_config_section__delete(section); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void perf_config_set__delete(struct perf_config_set *set) |
| 793 | { |
Taeung Song | 826424c | 2016-06-08 21:36:49 +0900 | [diff] [blame] | 794 | if (set == NULL) |
| 795 | return; |
| 796 | |
Taeung Song | 20105ca | 2016-04-14 16:53:18 +0900 | [diff] [blame] | 797 | perf_config_set__purge(set); |
| 798 | free(set); |
| 799 | } |
| 800 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 801 | /* |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 802 | * Call this to report error for your variable that should not |
| 803 | * get a boolean value (i.e. "[my] var" means "true"). |
| 804 | */ |
| 805 | int config_error_nonbool(const char *var) |
| 806 | { |
Arnaldo Carvalho de Melo | 62d94b0 | 2017-06-27 11:22:31 -0300 | [diff] [blame] | 807 | pr_err("Missing value for '%s'", var); |
| 808 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 809 | } |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 810 | |
Jiri Olsa | 99ce8e9 | 2014-12-01 20:06:24 +0100 | [diff] [blame] | 811 | void set_buildid_dir(const char *dir) |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 812 | { |
Jiri Olsa | 99ce8e9 | 2014-12-01 20:06:24 +0100 | [diff] [blame] | 813 | if (dir) |
| 814 | scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir); |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 815 | |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 816 | /* default to $HOME/.debug */ |
| 817 | if (buildid_dir[0] == '\0') { |
Taeung Song | 37194f4 | 2016-03-28 02:22:20 +0900 | [diff] [blame] | 818 | char *home = getenv("HOME"); |
| 819 | |
| 820 | if (home) { |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 821 | snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s", |
Taeung Song | 37194f4 | 2016-03-28 02:22:20 +0900 | [diff] [blame] | 822 | home, DEBUG_CACHE_DIR); |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 823 | } else { |
| 824 | strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1); |
| 825 | } |
| 826 | buildid_dir[MAXPATHLEN-1] = '\0'; |
| 827 | } |
| 828 | /* for communicating with external commands */ |
| 829 | setenv("PERF_BUILDID_DIR", buildid_dir, 1); |
| 830 | } |