186470930SIngo Molnar /* 25f9273d6SNamhyung Kim * config.c 35f9273d6SNamhyung Kim * 45f9273d6SNamhyung Kim * Helper functions for parsing config items. 55f9273d6SNamhyung Kim * Originally copied from GIT source. 686470930SIngo Molnar * 786470930SIngo Molnar * Copyright (C) Linus Torvalds, 2005 886470930SIngo Molnar * Copyright (C) Johannes Schindelin, 2005 986470930SIngo Molnar * 1086470930SIngo Molnar */ 11a43783aeSArnaldo Carvalho de Melo #include <errno.h> 12*391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 1386470930SIngo Molnar #include "util.h" 1486470930SIngo Molnar #include "cache.h" 154b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 160b93da17SNamhyung Kim #include "util/hist.h" /* perf_hist_config */ 17aa61fd05SWang Nan #include "util/llvm-utils.h" /* perf_llvm_config */ 1820105ca1STaeung Song #include "config.h" 1986470930SIngo Molnar 203d689ed6SArnaldo Carvalho de Melo #include "sane_ctype.h" 213d689ed6SArnaldo Carvalho de Melo 2286470930SIngo Molnar #define MAXNAME (256) 2386470930SIngo Molnar 2445de34bbSStephane Eranian #define DEBUG_CACHE_DIR ".debug" 2545de34bbSStephane Eranian 2645de34bbSStephane Eranian 2745de34bbSStephane Eranian char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ 2845de34bbSStephane Eranian 2986470930SIngo Molnar static FILE *config_file; 3086470930SIngo Molnar static const char *config_file_name; 3186470930SIngo Molnar static int config_linenr; 3286470930SIngo Molnar static int config_file_eof; 338a0a9c7eSTaeung Song static struct perf_config_set *config_set; 3486470930SIngo Molnar 35c7ac2417STaeung Song const char *config_exclusive_filename; 3686470930SIngo Molnar 3786470930SIngo Molnar static int get_next_char(void) 3886470930SIngo Molnar { 3986470930SIngo Molnar int c; 4086470930SIngo Molnar FILE *f; 4186470930SIngo Molnar 4286470930SIngo Molnar c = '\n'; 4386470930SIngo Molnar if ((f = config_file) != NULL) { 4486470930SIngo Molnar c = fgetc(f); 4586470930SIngo Molnar if (c == '\r') { 4686470930SIngo Molnar /* DOS like systems */ 4786470930SIngo Molnar c = fgetc(f); 4886470930SIngo Molnar if (c != '\n') { 4986470930SIngo Molnar ungetc(c, f); 5086470930SIngo Molnar c = '\r'; 5186470930SIngo Molnar } 5286470930SIngo Molnar } 5386470930SIngo Molnar if (c == '\n') 5486470930SIngo Molnar config_linenr++; 5586470930SIngo Molnar if (c == EOF) { 5686470930SIngo Molnar config_file_eof = 1; 5786470930SIngo Molnar c = '\n'; 5886470930SIngo Molnar } 5986470930SIngo Molnar } 6086470930SIngo Molnar return c; 6186470930SIngo Molnar } 6286470930SIngo Molnar 6386470930SIngo Molnar static char *parse_value(void) 6486470930SIngo Molnar { 6586470930SIngo Molnar static char value[1024]; 66f37a291cSIngo Molnar int quote = 0, comment = 0, space = 0; 67f37a291cSIngo Molnar size_t len = 0; 6886470930SIngo Molnar 6986470930SIngo Molnar for (;;) { 7086470930SIngo Molnar int c = get_next_char(); 71f37a291cSIngo Molnar 7286470930SIngo Molnar if (len >= sizeof(value) - 1) 7386470930SIngo Molnar return NULL; 7486470930SIngo Molnar if (c == '\n') { 7586470930SIngo Molnar if (quote) 7686470930SIngo Molnar return NULL; 7786470930SIngo Molnar value[len] = 0; 7886470930SIngo Molnar return value; 7986470930SIngo Molnar } 8086470930SIngo Molnar if (comment) 8186470930SIngo Molnar continue; 8286470930SIngo Molnar if (isspace(c) && !quote) { 8386470930SIngo Molnar space = 1; 8486470930SIngo Molnar continue; 8586470930SIngo Molnar } 8686470930SIngo Molnar if (!quote) { 8786470930SIngo Molnar if (c == ';' || c == '#') { 8886470930SIngo Molnar comment = 1; 8986470930SIngo Molnar continue; 9086470930SIngo Molnar } 9186470930SIngo Molnar } 9286470930SIngo Molnar if (space) { 9386470930SIngo Molnar if (len) 9486470930SIngo Molnar value[len++] = ' '; 9586470930SIngo Molnar space = 0; 9686470930SIngo Molnar } 9786470930SIngo Molnar if (c == '\\') { 9886470930SIngo Molnar c = get_next_char(); 9986470930SIngo Molnar switch (c) { 10086470930SIngo Molnar case '\n': 10186470930SIngo Molnar continue; 10286470930SIngo Molnar case 't': 10386470930SIngo Molnar c = '\t'; 10486470930SIngo Molnar break; 10586470930SIngo Molnar case 'b': 10686470930SIngo Molnar c = '\b'; 10786470930SIngo Molnar break; 10886470930SIngo Molnar case 'n': 10986470930SIngo Molnar c = '\n'; 11086470930SIngo Molnar break; 11186470930SIngo Molnar /* Some characters escape as themselves */ 11286470930SIngo Molnar case '\\': case '"': 11386470930SIngo Molnar break; 11486470930SIngo Molnar /* Reject unknown escape sequences */ 11586470930SIngo Molnar default: 11686470930SIngo Molnar return NULL; 11786470930SIngo Molnar } 11886470930SIngo Molnar value[len++] = c; 11986470930SIngo Molnar continue; 12086470930SIngo Molnar } 12186470930SIngo Molnar if (c == '"') { 12286470930SIngo Molnar quote = 1-quote; 12386470930SIngo Molnar continue; 12486470930SIngo Molnar } 12586470930SIngo Molnar value[len++] = c; 12686470930SIngo Molnar } 12786470930SIngo Molnar } 12886470930SIngo Molnar 12986470930SIngo Molnar static inline int iskeychar(int c) 13086470930SIngo Molnar { 1318dc7c651SArnaldo Carvalho de Melo return isalnum(c) || c == '-' || c == '_'; 13286470930SIngo Molnar } 13386470930SIngo Molnar 13486470930SIngo Molnar static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) 13586470930SIngo Molnar { 13686470930SIngo Molnar int c; 13786470930SIngo Molnar char *value; 13886470930SIngo Molnar 13986470930SIngo Molnar /* Get the full name */ 14086470930SIngo Molnar for (;;) { 14186470930SIngo Molnar c = get_next_char(); 14286470930SIngo Molnar if (config_file_eof) 14386470930SIngo Molnar break; 14486470930SIngo Molnar if (!iskeychar(c)) 14586470930SIngo Molnar break; 14645de34bbSStephane Eranian name[len++] = c; 14786470930SIngo Molnar if (len >= MAXNAME) 14886470930SIngo Molnar return -1; 14986470930SIngo Molnar } 15086470930SIngo Molnar name[len] = 0; 15186470930SIngo Molnar while (c == ' ' || c == '\t') 15286470930SIngo Molnar c = get_next_char(); 15386470930SIngo Molnar 15486470930SIngo Molnar value = NULL; 15586470930SIngo Molnar if (c != '\n') { 15686470930SIngo Molnar if (c != '=') 15786470930SIngo Molnar return -1; 15886470930SIngo Molnar value = parse_value(); 15986470930SIngo Molnar if (!value) 16086470930SIngo Molnar return -1; 16186470930SIngo Molnar } 16286470930SIngo Molnar return fn(name, value, data); 16386470930SIngo Molnar } 16486470930SIngo Molnar 16586470930SIngo Molnar static int get_extended_base_var(char *name, int baselen, int c) 16686470930SIngo Molnar { 16786470930SIngo Molnar do { 16886470930SIngo Molnar if (c == '\n') 16986470930SIngo Molnar return -1; 17086470930SIngo Molnar c = get_next_char(); 17186470930SIngo Molnar } while (isspace(c)); 17286470930SIngo Molnar 17386470930SIngo Molnar /* We require the format to be '[base "extension"]' */ 17486470930SIngo Molnar if (c != '"') 17586470930SIngo Molnar return -1; 17686470930SIngo Molnar name[baselen++] = '.'; 17786470930SIngo Molnar 17886470930SIngo Molnar for (;;) { 17983a0944fSIngo Molnar int ch = get_next_char(); 18083a0944fSIngo Molnar 18183a0944fSIngo Molnar if (ch == '\n') 18286470930SIngo Molnar return -1; 18383a0944fSIngo Molnar if (ch == '"') 18486470930SIngo Molnar break; 18583a0944fSIngo Molnar if (ch == '\\') { 18683a0944fSIngo Molnar ch = get_next_char(); 18783a0944fSIngo Molnar if (ch == '\n') 18886470930SIngo Molnar return -1; 18986470930SIngo Molnar } 19083a0944fSIngo Molnar name[baselen++] = ch; 19186470930SIngo Molnar if (baselen > MAXNAME / 2) 19286470930SIngo Molnar return -1; 19386470930SIngo Molnar } 19486470930SIngo Molnar 19586470930SIngo Molnar /* Final ']' */ 19686470930SIngo Molnar if (get_next_char() != ']') 19786470930SIngo Molnar return -1; 19886470930SIngo Molnar return baselen; 19986470930SIngo Molnar } 20086470930SIngo Molnar 20186470930SIngo Molnar static int get_base_var(char *name) 20286470930SIngo Molnar { 20386470930SIngo Molnar int baselen = 0; 20486470930SIngo Molnar 20586470930SIngo Molnar for (;;) { 20686470930SIngo Molnar int c = get_next_char(); 20786470930SIngo Molnar if (config_file_eof) 20886470930SIngo Molnar return -1; 20986470930SIngo Molnar if (c == ']') 21086470930SIngo Molnar return baselen; 21186470930SIngo Molnar if (isspace(c)) 21286470930SIngo Molnar return get_extended_base_var(name, baselen, c); 21386470930SIngo Molnar if (!iskeychar(c) && c != '.') 21486470930SIngo Molnar return -1; 21586470930SIngo Molnar if (baselen > MAXNAME / 2) 21686470930SIngo Molnar return -1; 21786470930SIngo Molnar name[baselen++] = tolower(c); 21886470930SIngo Molnar } 21986470930SIngo Molnar } 22086470930SIngo Molnar 22186470930SIngo Molnar static int perf_parse_file(config_fn_t fn, void *data) 22286470930SIngo Molnar { 22386470930SIngo Molnar int comment = 0; 22486470930SIngo Molnar int baselen = 0; 22586470930SIngo Molnar static char var[MAXNAME]; 22686470930SIngo Molnar 22786470930SIngo Molnar /* U+FEFF Byte Order Mark in UTF8 */ 22886470930SIngo Molnar static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; 22986470930SIngo Molnar const unsigned char *bomptr = utf8_bom; 23086470930SIngo Molnar 23186470930SIngo Molnar for (;;) { 23249757c9cSJiri Olsa int line, c = get_next_char(); 23349757c9cSJiri Olsa 23486470930SIngo Molnar if (bomptr && *bomptr) { 23586470930SIngo Molnar /* We are at the file beginning; skip UTF8-encoded BOM 23686470930SIngo Molnar * if present. Sane editors won't put this in on their 23786470930SIngo Molnar * own, but e.g. Windows Notepad will do it happily. */ 23886470930SIngo Molnar if ((unsigned char) c == *bomptr) { 23986470930SIngo Molnar bomptr++; 24086470930SIngo Molnar continue; 24186470930SIngo Molnar } else { 24286470930SIngo Molnar /* Do not tolerate partial BOM. */ 24386470930SIngo Molnar if (bomptr != utf8_bom) 24486470930SIngo Molnar break; 24586470930SIngo Molnar /* No BOM at file beginning. Cool. */ 24686470930SIngo Molnar bomptr = NULL; 24786470930SIngo Molnar } 24886470930SIngo Molnar } 24986470930SIngo Molnar if (c == '\n') { 25086470930SIngo Molnar if (config_file_eof) 25186470930SIngo Molnar return 0; 25286470930SIngo Molnar comment = 0; 25386470930SIngo Molnar continue; 25486470930SIngo Molnar } 25586470930SIngo Molnar if (comment || isspace(c)) 25686470930SIngo Molnar continue; 25786470930SIngo Molnar if (c == '#' || c == ';') { 25886470930SIngo Molnar comment = 1; 25986470930SIngo Molnar continue; 26086470930SIngo Molnar } 26186470930SIngo Molnar if (c == '[') { 26286470930SIngo Molnar baselen = get_base_var(var); 26386470930SIngo Molnar if (baselen <= 0) 26486470930SIngo Molnar break; 26586470930SIngo Molnar var[baselen++] = '.'; 26686470930SIngo Molnar var[baselen] = 0; 26786470930SIngo Molnar continue; 26886470930SIngo Molnar } 26986470930SIngo Molnar if (!isalpha(c)) 27086470930SIngo Molnar break; 27186470930SIngo Molnar var[baselen] = tolower(c); 27249757c9cSJiri Olsa 27349757c9cSJiri Olsa /* 27449757c9cSJiri Olsa * The get_value function might or might not reach the '\n', 27549757c9cSJiri Olsa * so saving the current line number for error reporting. 27649757c9cSJiri Olsa */ 27749757c9cSJiri Olsa line = config_linenr; 27849757c9cSJiri Olsa if (get_value(fn, data, var, baselen+1) < 0) { 27949757c9cSJiri Olsa config_linenr = line; 28086470930SIngo Molnar break; 28186470930SIngo Molnar } 28249757c9cSJiri Olsa } 28378f71c99STaeung Song pr_err("bad config file line %d in %s\n", config_linenr, config_file_name); 28478f71c99STaeung Song return -1; 28586470930SIngo Molnar } 28686470930SIngo Molnar 28786470930SIngo Molnar static int parse_unit_factor(const char *end, unsigned long *val) 28886470930SIngo Molnar { 28986470930SIngo Molnar if (!*end) 29086470930SIngo Molnar return 1; 29186470930SIngo Molnar else if (!strcasecmp(end, "k")) { 29286470930SIngo Molnar *val *= 1024; 29386470930SIngo Molnar return 1; 29486470930SIngo Molnar } 29586470930SIngo Molnar else if (!strcasecmp(end, "m")) { 29686470930SIngo Molnar *val *= 1024 * 1024; 29786470930SIngo Molnar return 1; 29886470930SIngo Molnar } 29986470930SIngo Molnar else if (!strcasecmp(end, "g")) { 30086470930SIngo Molnar *val *= 1024 * 1024 * 1024; 30186470930SIngo Molnar return 1; 30286470930SIngo Molnar } 30386470930SIngo Molnar return 0; 30486470930SIngo Molnar } 30586470930SIngo Molnar 30694c0655fSJiri Olsa static int perf_parse_llong(const char *value, long long *ret) 30794c0655fSJiri Olsa { 30894c0655fSJiri Olsa if (value && *value) { 30994c0655fSJiri Olsa char *end; 31094c0655fSJiri Olsa long long val = strtoll(value, &end, 0); 31194c0655fSJiri Olsa unsigned long factor = 1; 31294c0655fSJiri Olsa 31394c0655fSJiri Olsa if (!parse_unit_factor(end, &factor)) 31494c0655fSJiri Olsa return 0; 31594c0655fSJiri Olsa *ret = val * factor; 31694c0655fSJiri Olsa return 1; 31794c0655fSJiri Olsa } 31894c0655fSJiri Olsa return 0; 31994c0655fSJiri Olsa } 32094c0655fSJiri Olsa 32186470930SIngo Molnar static int perf_parse_long(const char *value, long *ret) 32286470930SIngo Molnar { 32386470930SIngo Molnar if (value && *value) { 32486470930SIngo Molnar char *end; 32586470930SIngo Molnar long val = strtol(value, &end, 0); 32686470930SIngo Molnar unsigned long factor = 1; 32786470930SIngo Molnar if (!parse_unit_factor(end, &factor)) 32886470930SIngo Molnar return 0; 32986470930SIngo Molnar *ret = val * factor; 33086470930SIngo Molnar return 1; 33186470930SIngo Molnar } 33286470930SIngo Molnar return 0; 33386470930SIngo Molnar } 33486470930SIngo Molnar 33586470930SIngo Molnar static void die_bad_config(const char *name) 33686470930SIngo Molnar { 33786470930SIngo Molnar if (config_file_name) 33886470930SIngo Molnar die("bad config value for '%s' in %s", name, config_file_name); 33986470930SIngo Molnar die("bad config value for '%s'", name); 34086470930SIngo Molnar } 34186470930SIngo Molnar 34294c0655fSJiri Olsa u64 perf_config_u64(const char *name, const char *value) 34394c0655fSJiri Olsa { 34494c0655fSJiri Olsa long long ret = 0; 34594c0655fSJiri Olsa 34694c0655fSJiri Olsa if (!perf_parse_llong(value, &ret)) 34794c0655fSJiri Olsa die_bad_config(name); 34894c0655fSJiri Olsa return (u64) ret; 34994c0655fSJiri Olsa } 35094c0655fSJiri Olsa 35186470930SIngo Molnar int perf_config_int(const char *name, const char *value) 35286470930SIngo Molnar { 35386470930SIngo Molnar long ret = 0; 35486470930SIngo Molnar if (!perf_parse_long(value, &ret)) 35586470930SIngo Molnar die_bad_config(name); 35686470930SIngo Molnar return ret; 35786470930SIngo Molnar } 35886470930SIngo Molnar 359a41794cdSArnaldo Carvalho de Melo static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool) 36086470930SIngo Molnar { 36186470930SIngo Molnar *is_bool = 1; 36286470930SIngo Molnar if (!value) 36386470930SIngo Molnar return 1; 36486470930SIngo Molnar if (!*value) 36586470930SIngo Molnar return 0; 36686470930SIngo Molnar if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on")) 36786470930SIngo Molnar return 1; 36886470930SIngo Molnar if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off")) 36986470930SIngo Molnar return 0; 37086470930SIngo Molnar *is_bool = 0; 37186470930SIngo Molnar return perf_config_int(name, value); 37286470930SIngo Molnar } 37386470930SIngo Molnar 37486470930SIngo Molnar int perf_config_bool(const char *name, const char *value) 37586470930SIngo Molnar { 37686470930SIngo Molnar int discard; 37786470930SIngo Molnar return !!perf_config_bool_or_int(name, value, &discard); 37886470930SIngo Molnar } 37986470930SIngo Molnar 380814b3f51SArnaldo Carvalho de Melo static const char *perf_config_dirname(const char *name, const char *value) 38145de34bbSStephane Eranian { 38245de34bbSStephane Eranian if (!name) 38345de34bbSStephane Eranian return NULL; 38445de34bbSStephane Eranian return value; 38545de34bbSStephane Eranian } 38645de34bbSStephane Eranian 3879cb5987cSTaeung Song static int perf_buildid_config(const char *var, const char *value) 3889cb5987cSTaeung Song { 3899cb5987cSTaeung Song /* same dir for all commands */ 3909cb5987cSTaeung Song if (!strcmp(var, "buildid.dir")) { 391d8e28654SVinson Lee const char *dir = perf_config_dirname(var, value); 3929cb5987cSTaeung Song 393ecc4c561SArnaldo Carvalho de Melo if (!dir) { 394ecc4c561SArnaldo Carvalho de Melo pr_err("Invalid buildid directory!\n"); 3959cb5987cSTaeung Song return -1; 396ecc4c561SArnaldo Carvalho de Melo } 397d8e28654SVinson Lee strncpy(buildid_dir, dir, MAXPATHLEN-1); 3989cb5987cSTaeung Song buildid_dir[MAXPATHLEN-1] = '\0'; 3999cb5987cSTaeung Song } 4009cb5987cSTaeung Song 4019cb5987cSTaeung Song return 0; 4029cb5987cSTaeung Song } 4039cb5987cSTaeung Song 4041d037ca1SIrina Tirdea static int perf_default_core_config(const char *var __maybe_unused, 4051d037ca1SIrina Tirdea const char *value __maybe_unused) 40686470930SIngo Molnar { 407395cf969SPaul Bolle /* Add other config variables here. */ 40886470930SIngo Molnar return 0; 40986470930SIngo Molnar } 41086470930SIngo Molnar 411c8302367SJiri Olsa static int perf_ui_config(const char *var, const char *value) 412c8302367SJiri Olsa { 413c8302367SJiri Olsa /* Add other config variables here. */ 414ecc4c561SArnaldo Carvalho de Melo if (!strcmp(var, "ui.show-headers")) 415c8302367SJiri Olsa symbol_conf.show_hist_headers = perf_config_bool(var, value); 416ecc4c561SArnaldo Carvalho de Melo 417c8302367SJiri Olsa return 0; 418c8302367SJiri Olsa } 419c8302367SJiri Olsa 4201d037ca1SIrina Tirdea int perf_default_config(const char *var, const char *value, 4211d037ca1SIrina Tirdea void *dummy __maybe_unused) 42286470930SIngo Molnar { 42386470930SIngo Molnar if (!prefixcmp(var, "core.")) 42486470930SIngo Molnar return perf_default_core_config(var, value); 42586470930SIngo Molnar 4260b93da17SNamhyung Kim if (!prefixcmp(var, "hist.")) 4270b93da17SNamhyung Kim return perf_hist_config(var, value); 4280b93da17SNamhyung Kim 429c8302367SJiri Olsa if (!prefixcmp(var, "ui.")) 430c8302367SJiri Olsa return perf_ui_config(var, value); 431c8302367SJiri Olsa 4322b9240caSNamhyung Kim if (!prefixcmp(var, "call-graph.")) 4332b9240caSNamhyung Kim return perf_callchain_config(var, value); 4342b9240caSNamhyung Kim 435aa61fd05SWang Nan if (!prefixcmp(var, "llvm.")) 436aa61fd05SWang Nan return perf_llvm_config(var, value); 437aa61fd05SWang Nan 4389cb5987cSTaeung Song if (!prefixcmp(var, "buildid.")) 4399cb5987cSTaeung Song return perf_buildid_config(var, value); 4409cb5987cSTaeung Song 441395cf969SPaul Bolle /* Add other config variables here. */ 44286470930SIngo Molnar return 0; 44386470930SIngo Molnar } 44486470930SIngo Molnar 445a41794cdSArnaldo Carvalho de Melo static int perf_config_from_file(config_fn_t fn, const char *filename, void *data) 44686470930SIngo Molnar { 44786470930SIngo Molnar int ret; 44886470930SIngo Molnar FILE *f = fopen(filename, "r"); 44986470930SIngo Molnar 45086470930SIngo Molnar ret = -1; 45186470930SIngo Molnar if (f) { 45286470930SIngo Molnar config_file = f; 45386470930SIngo Molnar config_file_name = filename; 45486470930SIngo Molnar config_linenr = 1; 45586470930SIngo Molnar config_file_eof = 0; 45686470930SIngo Molnar ret = perf_parse_file(fn, data); 45786470930SIngo Molnar fclose(f); 45886470930SIngo Molnar config_file_name = NULL; 45986470930SIngo Molnar } 46086470930SIngo Molnar return ret; 46186470930SIngo Molnar } 46286470930SIngo Molnar 463c7ac2417STaeung Song const char *perf_etc_perfconfig(void) 46486470930SIngo Molnar { 46586470930SIngo Molnar static const char *system_wide; 46686470930SIngo Molnar if (!system_wide) 46786470930SIngo Molnar system_wide = system_path(ETC_PERFCONFIG); 46886470930SIngo Molnar return system_wide; 46986470930SIngo Molnar } 47086470930SIngo Molnar 47186470930SIngo Molnar static int perf_env_bool(const char *k, int def) 47286470930SIngo Molnar { 47386470930SIngo Molnar const char *v = getenv(k); 47486470930SIngo Molnar return v ? perf_config_bool(k, v) : def; 47586470930SIngo Molnar } 47686470930SIngo Molnar 477a41794cdSArnaldo Carvalho de Melo static int perf_config_system(void) 47886470930SIngo Molnar { 47986470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); 48086470930SIngo Molnar } 48186470930SIngo Molnar 482a41794cdSArnaldo Carvalho de Melo static int perf_config_global(void) 48386470930SIngo Molnar { 48486470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); 48586470930SIngo Molnar } 48686470930SIngo Molnar 48720105ca1STaeung Song static struct perf_config_section *find_section(struct list_head *sections, 48820105ca1STaeung Song const char *section_name) 48920105ca1STaeung Song { 49020105ca1STaeung Song struct perf_config_section *section; 49120105ca1STaeung Song 49220105ca1STaeung Song list_for_each_entry(section, sections, node) 49320105ca1STaeung Song if (!strcmp(section->name, section_name)) 49420105ca1STaeung Song return section; 49520105ca1STaeung Song 49620105ca1STaeung Song return NULL; 49720105ca1STaeung Song } 49820105ca1STaeung Song 49920105ca1STaeung Song static struct perf_config_item *find_config_item(const char *name, 50020105ca1STaeung Song struct perf_config_section *section) 50120105ca1STaeung Song { 50220105ca1STaeung Song struct perf_config_item *item; 50320105ca1STaeung Song 50420105ca1STaeung Song list_for_each_entry(item, §ion->items, node) 50520105ca1STaeung Song if (!strcmp(item->name, name)) 50620105ca1STaeung Song return item; 50720105ca1STaeung Song 50820105ca1STaeung Song return NULL; 50920105ca1STaeung Song } 51020105ca1STaeung Song 51120105ca1STaeung Song static struct perf_config_section *add_section(struct list_head *sections, 51220105ca1STaeung Song const char *section_name) 51320105ca1STaeung Song { 51420105ca1STaeung Song struct perf_config_section *section = zalloc(sizeof(*section)); 51520105ca1STaeung Song 51620105ca1STaeung Song if (!section) 51720105ca1STaeung Song return NULL; 51820105ca1STaeung Song 51920105ca1STaeung Song INIT_LIST_HEAD(§ion->items); 52020105ca1STaeung Song section->name = strdup(section_name); 52120105ca1STaeung Song if (!section->name) { 52220105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 52320105ca1STaeung Song free(section); 52420105ca1STaeung Song return NULL; 52520105ca1STaeung Song } 52620105ca1STaeung Song 52720105ca1STaeung Song list_add_tail(§ion->node, sections); 52820105ca1STaeung Song return section; 52920105ca1STaeung Song } 53020105ca1STaeung Song 53120105ca1STaeung Song static struct perf_config_item *add_config_item(struct perf_config_section *section, 53220105ca1STaeung Song const char *name) 53320105ca1STaeung Song { 53420105ca1STaeung Song struct perf_config_item *item = zalloc(sizeof(*item)); 53520105ca1STaeung Song 53620105ca1STaeung Song if (!item) 53720105ca1STaeung Song return NULL; 53820105ca1STaeung Song 53920105ca1STaeung Song item->name = strdup(name); 54020105ca1STaeung Song if (!item->name) { 54120105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 54220105ca1STaeung Song free(item); 54320105ca1STaeung Song return NULL; 54420105ca1STaeung Song } 54520105ca1STaeung Song 54620105ca1STaeung Song list_add_tail(&item->node, §ion->items); 54720105ca1STaeung Song return item; 54820105ca1STaeung Song } 54920105ca1STaeung Song 55020105ca1STaeung Song static int set_value(struct perf_config_item *item, const char *value) 55120105ca1STaeung Song { 55220105ca1STaeung Song char *val = strdup(value); 55320105ca1STaeung Song 55420105ca1STaeung Song if (!val) 55520105ca1STaeung Song return -1; 55620105ca1STaeung Song 55720105ca1STaeung Song zfree(&item->value); 55820105ca1STaeung Song item->value = val; 55920105ca1STaeung Song return 0; 56020105ca1STaeung Song } 56120105ca1STaeung Song 56220105ca1STaeung Song static int collect_config(const char *var, const char *value, 56320105ca1STaeung Song void *perf_config_set) 56420105ca1STaeung Song { 56520105ca1STaeung Song int ret = -1; 56620105ca1STaeung Song char *ptr, *key; 56720105ca1STaeung Song char *section_name, *name; 56820105ca1STaeung Song struct perf_config_section *section = NULL; 56920105ca1STaeung Song struct perf_config_item *item = NULL; 57020105ca1STaeung Song struct perf_config_set *set = perf_config_set; 5717db91f25STaeung Song struct list_head *sections; 57220105ca1STaeung Song 5737db91f25STaeung Song if (set == NULL) 5747db91f25STaeung Song return -1; 5757db91f25STaeung Song 5767db91f25STaeung Song sections = &set->sections; 57720105ca1STaeung Song key = ptr = strdup(var); 57820105ca1STaeung Song if (!key) { 57920105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 58020105ca1STaeung Song return -1; 58120105ca1STaeung Song } 58220105ca1STaeung Song 58320105ca1STaeung Song section_name = strsep(&ptr, "."); 58420105ca1STaeung Song name = ptr; 58520105ca1STaeung Song if (name == NULL || value == NULL) 58620105ca1STaeung Song goto out_free; 58720105ca1STaeung Song 58820105ca1STaeung Song section = find_section(sections, section_name); 58920105ca1STaeung Song if (!section) { 59020105ca1STaeung Song section = add_section(sections, section_name); 59120105ca1STaeung Song if (!section) 59220105ca1STaeung Song goto out_free; 59320105ca1STaeung Song } 59420105ca1STaeung Song 59520105ca1STaeung Song item = find_config_item(name, section); 59620105ca1STaeung Song if (!item) { 59720105ca1STaeung Song item = add_config_item(section, name); 59820105ca1STaeung Song if (!item) 59920105ca1STaeung Song goto out_free; 60020105ca1STaeung Song } 60120105ca1STaeung Song 60208d090cfSTaeung Song /* perf_config_set can contain both user and system config items. 60308d090cfSTaeung Song * So we should know where each value is from. 60408d090cfSTaeung Song * The classification would be needed when a particular config file 60508d090cfSTaeung Song * is overwrited by setting feature i.e. set_config(). 60608d090cfSTaeung Song */ 60708d090cfSTaeung Song if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) { 60808d090cfSTaeung Song section->from_system_config = true; 60908d090cfSTaeung Song item->from_system_config = true; 61008d090cfSTaeung Song } else { 61108d090cfSTaeung Song section->from_system_config = false; 61208d090cfSTaeung Song item->from_system_config = false; 61308d090cfSTaeung Song } 61408d090cfSTaeung Song 61520105ca1STaeung Song ret = set_value(item, value); 61620105ca1STaeung Song return ret; 61720105ca1STaeung Song 61820105ca1STaeung Song out_free: 61920105ca1STaeung Song free(key); 62020105ca1STaeung Song return -1; 62120105ca1STaeung Song } 62220105ca1STaeung Song 62308d090cfSTaeung Song int perf_config_set__collect(struct perf_config_set *set, const char *file_name, 624c6fc018aSTaeung Song const char *var, const char *value) 625c6fc018aSTaeung Song { 62608d090cfSTaeung Song config_file_name = file_name; 627c6fc018aSTaeung Song return collect_config(var, value, set); 628c6fc018aSTaeung Song } 629c6fc018aSTaeung Song 6308beeb00fSTaeung Song static int perf_config_set__init(struct perf_config_set *set) 6318beeb00fSTaeung Song { 6328beeb00fSTaeung Song int ret = -1; 6338beeb00fSTaeung Song const char *home = NULL; 6343e00cbe8SJiri Olsa char *user_config; 6353e00cbe8SJiri Olsa struct stat st; 6368beeb00fSTaeung Song 6378beeb00fSTaeung Song /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ 6388beeb00fSTaeung Song if (config_exclusive_filename) 6398beeb00fSTaeung Song return perf_config_from_file(collect_config, config_exclusive_filename, set); 6408beeb00fSTaeung Song if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) { 6418beeb00fSTaeung Song if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0) 6428beeb00fSTaeung Song goto out; 6438beeb00fSTaeung Song } 6448beeb00fSTaeung Song 6458beeb00fSTaeung Song home = getenv("HOME"); 6468beeb00fSTaeung Song 6473e00cbe8SJiri Olsa /* 6483e00cbe8SJiri Olsa * Skip reading user config if: 6493e00cbe8SJiri Olsa * - there is no place to read it from (HOME) 6503e00cbe8SJiri Olsa * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) 6513e00cbe8SJiri Olsa */ 6523e00cbe8SJiri Olsa if (!home || !*home || !perf_config_global()) 6533e00cbe8SJiri Olsa return 0; 6543e00cbe8SJiri Olsa 6553e00cbe8SJiri Olsa user_config = strdup(mkpath("%s/.perfconfig", home)); 6568beeb00fSTaeung Song if (user_config == NULL) { 6578beeb00fSTaeung Song warning("Not enough memory to process %s/.perfconfig, " 6588beeb00fSTaeung Song "ignoring it.", home); 6598beeb00fSTaeung Song goto out; 6608beeb00fSTaeung Song } 6618beeb00fSTaeung Song 662afc45cf5SArnaldo Carvalho de Melo if (stat(user_config, &st) < 0) { 663afc45cf5SArnaldo Carvalho de Melo if (errno == ENOENT) 664afc45cf5SArnaldo Carvalho de Melo ret = 0; 6658beeb00fSTaeung Song goto out_free; 666afc45cf5SArnaldo Carvalho de Melo } 667afc45cf5SArnaldo Carvalho de Melo 668afc45cf5SArnaldo Carvalho de Melo ret = 0; 6698beeb00fSTaeung Song 6708beeb00fSTaeung Song if (st.st_uid && (st.st_uid != geteuid())) { 6718beeb00fSTaeung Song warning("File %s not owned by current user or root, " 6728beeb00fSTaeung Song "ignoring it.", user_config); 6738beeb00fSTaeung Song goto out_free; 6748beeb00fSTaeung Song } 6758beeb00fSTaeung Song 676afc45cf5SArnaldo Carvalho de Melo if (st.st_size) 6778beeb00fSTaeung Song ret = perf_config_from_file(collect_config, user_config, set); 6783e00cbe8SJiri Olsa 6798beeb00fSTaeung Song out_free: 6808beeb00fSTaeung Song free(user_config); 6818beeb00fSTaeung Song out: 6828beeb00fSTaeung Song return ret; 6838beeb00fSTaeung Song } 6848beeb00fSTaeung Song 68520105ca1STaeung Song struct perf_config_set *perf_config_set__new(void) 68620105ca1STaeung Song { 68720105ca1STaeung Song struct perf_config_set *set = zalloc(sizeof(*set)); 68820105ca1STaeung Song 68920105ca1STaeung Song if (set) { 69020105ca1STaeung Song INIT_LIST_HEAD(&set->sections); 6918beeb00fSTaeung Song if (perf_config_set__init(set) < 0) { 69225d8f48fSTaeung Song perf_config_set__delete(set); 69325d8f48fSTaeung Song set = NULL; 69425d8f48fSTaeung Song } 69520105ca1STaeung Song } 69620105ca1STaeung Song 69720105ca1STaeung Song return set; 69820105ca1STaeung Song } 69920105ca1STaeung Song 7008a0a9c7eSTaeung Song int perf_config(config_fn_t fn, void *data) 7018a0a9c7eSTaeung Song { 7028a0a9c7eSTaeung Song int ret = 0; 7038a0a9c7eSTaeung Song char key[BUFSIZ]; 7048a0a9c7eSTaeung Song struct perf_config_section *section; 7058a0a9c7eSTaeung Song struct perf_config_item *item; 7068a0a9c7eSTaeung Song 7078a0a9c7eSTaeung Song if (config_set == NULL) 7088a0a9c7eSTaeung Song return -1; 7098a0a9c7eSTaeung Song 7108a0a9c7eSTaeung Song perf_config_set__for_each_entry(config_set, section, item) { 7118a0a9c7eSTaeung Song char *value = item->value; 7128a0a9c7eSTaeung Song 7138a0a9c7eSTaeung Song if (value) { 7148a0a9c7eSTaeung Song scnprintf(key, sizeof(key), "%s.%s", 7158a0a9c7eSTaeung Song section->name, item->name); 7168a0a9c7eSTaeung Song ret = fn(key, value, data); 7178a0a9c7eSTaeung Song if (ret < 0) { 7188a0a9c7eSTaeung Song pr_err("Error: wrong config key-value pair %s=%s\n", 7198a0a9c7eSTaeung Song key, value); 7208a0a9c7eSTaeung Song break; 7218a0a9c7eSTaeung Song } 7228a0a9c7eSTaeung Song } 7238a0a9c7eSTaeung Song } 7248a0a9c7eSTaeung Song 7258a0a9c7eSTaeung Song return ret; 7268a0a9c7eSTaeung Song } 7278a0a9c7eSTaeung Song 7288a0a9c7eSTaeung Song void perf_config__init(void) 7298a0a9c7eSTaeung Song { 7308a0a9c7eSTaeung Song if (config_set == NULL) 7318a0a9c7eSTaeung Song config_set = perf_config_set__new(); 7328a0a9c7eSTaeung Song } 7338a0a9c7eSTaeung Song 7348a0a9c7eSTaeung Song void perf_config__exit(void) 7358a0a9c7eSTaeung Song { 7368a0a9c7eSTaeung Song perf_config_set__delete(config_set); 7378a0a9c7eSTaeung Song config_set = NULL; 7388a0a9c7eSTaeung Song } 7398a0a9c7eSTaeung Song 7408a0a9c7eSTaeung Song void perf_config__refresh(void) 7418a0a9c7eSTaeung Song { 7428a0a9c7eSTaeung Song perf_config__exit(); 7438a0a9c7eSTaeung Song perf_config__init(); 7448a0a9c7eSTaeung Song } 7458a0a9c7eSTaeung Song 74620105ca1STaeung Song static void perf_config_item__delete(struct perf_config_item *item) 74720105ca1STaeung Song { 74820105ca1STaeung Song zfree(&item->name); 74920105ca1STaeung Song zfree(&item->value); 75020105ca1STaeung Song free(item); 75120105ca1STaeung Song } 75220105ca1STaeung Song 75320105ca1STaeung Song static void perf_config_section__purge(struct perf_config_section *section) 75420105ca1STaeung Song { 75520105ca1STaeung Song struct perf_config_item *item, *tmp; 75620105ca1STaeung Song 75720105ca1STaeung Song list_for_each_entry_safe(item, tmp, §ion->items, node) { 75820105ca1STaeung Song list_del_init(&item->node); 75920105ca1STaeung Song perf_config_item__delete(item); 76020105ca1STaeung Song } 76120105ca1STaeung Song } 76220105ca1STaeung Song 76320105ca1STaeung Song static void perf_config_section__delete(struct perf_config_section *section) 76420105ca1STaeung Song { 76520105ca1STaeung Song perf_config_section__purge(section); 76620105ca1STaeung Song zfree(§ion->name); 76720105ca1STaeung Song free(section); 76820105ca1STaeung Song } 76920105ca1STaeung Song 77020105ca1STaeung Song static void perf_config_set__purge(struct perf_config_set *set) 77120105ca1STaeung Song { 77220105ca1STaeung Song struct perf_config_section *section, *tmp; 77320105ca1STaeung Song 77420105ca1STaeung Song list_for_each_entry_safe(section, tmp, &set->sections, node) { 77520105ca1STaeung Song list_del_init(§ion->node); 77620105ca1STaeung Song perf_config_section__delete(section); 77720105ca1STaeung Song } 77820105ca1STaeung Song } 77920105ca1STaeung Song 78020105ca1STaeung Song void perf_config_set__delete(struct perf_config_set *set) 78120105ca1STaeung Song { 782826424ccSTaeung Song if (set == NULL) 783826424ccSTaeung Song return; 784826424ccSTaeung Song 78520105ca1STaeung Song perf_config_set__purge(set); 78620105ca1STaeung Song free(set); 78720105ca1STaeung Song } 78820105ca1STaeung Song 78986470930SIngo Molnar /* 79086470930SIngo Molnar * Call this to report error for your variable that should not 79186470930SIngo Molnar * get a boolean value (i.e. "[my] var" means "true"). 79286470930SIngo Molnar */ 79386470930SIngo Molnar int config_error_nonbool(const char *var) 79486470930SIngo Molnar { 79586470930SIngo Molnar return error("Missing value for '%s'", var); 79686470930SIngo Molnar } 79745de34bbSStephane Eranian 79899ce8e9fSJiri Olsa void set_buildid_dir(const char *dir) 79945de34bbSStephane Eranian { 80099ce8e9fSJiri Olsa if (dir) 80199ce8e9fSJiri Olsa scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir); 80245de34bbSStephane Eranian 80345de34bbSStephane Eranian /* default to $HOME/.debug */ 80445de34bbSStephane Eranian if (buildid_dir[0] == '\0') { 80537194f44STaeung Song char *home = getenv("HOME"); 80637194f44STaeung Song 80737194f44STaeung Song if (home) { 80845de34bbSStephane Eranian snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s", 80937194f44STaeung Song home, DEBUG_CACHE_DIR); 81045de34bbSStephane Eranian } else { 81145de34bbSStephane Eranian strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1); 81245de34bbSStephane Eranian } 81345de34bbSStephane Eranian buildid_dir[MAXPATHLEN-1] = '\0'; 81445de34bbSStephane Eranian } 81545de34bbSStephane Eranian /* for communicating with external commands */ 81645de34bbSStephane Eranian setenv("PERF_BUILDID_DIR", buildid_dir, 1); 81745de34bbSStephane Eranian } 818