1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286470930SIngo Molnar /* 35f9273d6SNamhyung Kim * config.c 45f9273d6SNamhyung Kim * 55f9273d6SNamhyung Kim * Helper functions for parsing config items. 65f9273d6SNamhyung Kim * Originally copied from GIT source. 786470930SIngo Molnar * 886470930SIngo Molnar * Copyright (C) Linus Torvalds, 2005 986470930SIngo Molnar * Copyright (C) Johannes Schindelin, 2005 1086470930SIngo Molnar * 1186470930SIngo Molnar */ 12a43783aeSArnaldo Carvalho de Melo #include <errno.h> 13391e4206SArnaldo Carvalho de Melo #include <sys/param.h> 1486470930SIngo Molnar #include "cache.h" 15b10ba7f1SArnaldo Carvalho de Melo #include "callchain.h" 164b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h> 173fcb10e4SMark Drayton #include "util/event.h" /* proc_map_timeout */ 180b93da17SNamhyung Kim #include "util/hist.h" /* perf_hist_config */ 19aa61fd05SWang Nan #include "util/llvm-utils.h" /* perf_llvm_config */ 204cb3c6d5SArnaldo Carvalho de Melo #include "build-id.h" 214cb3c6d5SArnaldo Carvalho de Melo #include "debug.h" 2220105ca1STaeung Song #include "config.h" 23b4209025SArnaldo Carvalho de Melo #include "debug.h" 247a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h> 257a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 26*f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 277a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h> 288e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h> 297f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 303052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h> 313d689ed6SArnaldo Carvalho de Melo 3286470930SIngo Molnar #define MAXNAME (256) 3386470930SIngo Molnar 3445de34bbSStephane Eranian #define DEBUG_CACHE_DIR ".debug" 3545de34bbSStephane Eranian 3645de34bbSStephane Eranian 3745de34bbSStephane Eranian char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ 3845de34bbSStephane Eranian 3986470930SIngo Molnar static FILE *config_file; 4086470930SIngo Molnar static const char *config_file_name; 4186470930SIngo Molnar static int config_linenr; 4286470930SIngo Molnar static int config_file_eof; 438a0a9c7eSTaeung Song static struct perf_config_set *config_set; 4486470930SIngo Molnar 45c7ac2417STaeung Song const char *config_exclusive_filename; 4686470930SIngo Molnar 4786470930SIngo Molnar static int get_next_char(void) 4886470930SIngo Molnar { 4986470930SIngo Molnar int c; 5086470930SIngo Molnar FILE *f; 5186470930SIngo Molnar 5286470930SIngo Molnar c = '\n'; 5386470930SIngo Molnar if ((f = config_file) != NULL) { 5486470930SIngo Molnar c = fgetc(f); 5586470930SIngo Molnar if (c == '\r') { 5686470930SIngo Molnar /* DOS like systems */ 5786470930SIngo Molnar c = fgetc(f); 5886470930SIngo Molnar if (c != '\n') { 5986470930SIngo Molnar ungetc(c, f); 6086470930SIngo Molnar c = '\r'; 6186470930SIngo Molnar } 6286470930SIngo Molnar } 6386470930SIngo Molnar if (c == '\n') 6486470930SIngo Molnar config_linenr++; 6586470930SIngo Molnar if (c == EOF) { 6686470930SIngo Molnar config_file_eof = 1; 6786470930SIngo Molnar c = '\n'; 6886470930SIngo Molnar } 6986470930SIngo Molnar } 7086470930SIngo Molnar return c; 7186470930SIngo Molnar } 7286470930SIngo Molnar 7386470930SIngo Molnar static char *parse_value(void) 7486470930SIngo Molnar { 7586470930SIngo Molnar static char value[1024]; 76f37a291cSIngo Molnar int quote = 0, comment = 0, space = 0; 77f37a291cSIngo Molnar size_t len = 0; 7886470930SIngo Molnar 7986470930SIngo Molnar for (;;) { 8086470930SIngo Molnar int c = get_next_char(); 81f37a291cSIngo Molnar 8286470930SIngo Molnar if (len >= sizeof(value) - 1) 8386470930SIngo Molnar return NULL; 8486470930SIngo Molnar if (c == '\n') { 8586470930SIngo Molnar if (quote) 8686470930SIngo Molnar return NULL; 8786470930SIngo Molnar value[len] = 0; 8886470930SIngo Molnar return value; 8986470930SIngo Molnar } 9086470930SIngo Molnar if (comment) 9186470930SIngo Molnar continue; 9286470930SIngo Molnar if (isspace(c) && !quote) { 9386470930SIngo Molnar space = 1; 9486470930SIngo Molnar continue; 9586470930SIngo Molnar } 9686470930SIngo Molnar if (!quote) { 9786470930SIngo Molnar if (c == ';' || c == '#') { 9886470930SIngo Molnar comment = 1; 9986470930SIngo Molnar continue; 10086470930SIngo Molnar } 10186470930SIngo Molnar } 10286470930SIngo Molnar if (space) { 10386470930SIngo Molnar if (len) 10486470930SIngo Molnar value[len++] = ' '; 10586470930SIngo Molnar space = 0; 10686470930SIngo Molnar } 10786470930SIngo Molnar if (c == '\\') { 10886470930SIngo Molnar c = get_next_char(); 10986470930SIngo Molnar switch (c) { 11086470930SIngo Molnar case '\n': 11186470930SIngo Molnar continue; 11286470930SIngo Molnar case 't': 11386470930SIngo Molnar c = '\t'; 11486470930SIngo Molnar break; 11586470930SIngo Molnar case 'b': 11686470930SIngo Molnar c = '\b'; 11786470930SIngo Molnar break; 11886470930SIngo Molnar case 'n': 11986470930SIngo Molnar c = '\n'; 12086470930SIngo Molnar break; 12186470930SIngo Molnar /* Some characters escape as themselves */ 12286470930SIngo Molnar case '\\': case '"': 12386470930SIngo Molnar break; 12486470930SIngo Molnar /* Reject unknown escape sequences */ 12586470930SIngo Molnar default: 12686470930SIngo Molnar return NULL; 12786470930SIngo Molnar } 12886470930SIngo Molnar value[len++] = c; 12986470930SIngo Molnar continue; 13086470930SIngo Molnar } 13186470930SIngo Molnar if (c == '"') { 13286470930SIngo Molnar quote = 1-quote; 13386470930SIngo Molnar continue; 13486470930SIngo Molnar } 13586470930SIngo Molnar value[len++] = c; 13686470930SIngo Molnar } 13786470930SIngo Molnar } 13886470930SIngo Molnar 13986470930SIngo Molnar static inline int iskeychar(int c) 14086470930SIngo Molnar { 1418dc7c651SArnaldo Carvalho de Melo return isalnum(c) || c == '-' || c == '_'; 14286470930SIngo Molnar } 14386470930SIngo Molnar 14486470930SIngo Molnar static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) 14586470930SIngo Molnar { 14686470930SIngo Molnar int c; 14786470930SIngo Molnar char *value; 14886470930SIngo Molnar 14986470930SIngo Molnar /* Get the full name */ 15086470930SIngo Molnar for (;;) { 15186470930SIngo Molnar c = get_next_char(); 15286470930SIngo Molnar if (config_file_eof) 15386470930SIngo Molnar break; 15486470930SIngo Molnar if (!iskeychar(c)) 15586470930SIngo Molnar break; 15645de34bbSStephane Eranian name[len++] = c; 15786470930SIngo Molnar if (len >= MAXNAME) 15886470930SIngo Molnar return -1; 15986470930SIngo Molnar } 16086470930SIngo Molnar name[len] = 0; 16186470930SIngo Molnar while (c == ' ' || c == '\t') 16286470930SIngo Molnar c = get_next_char(); 16386470930SIngo Molnar 16486470930SIngo Molnar value = NULL; 16586470930SIngo Molnar if (c != '\n') { 16686470930SIngo Molnar if (c != '=') 16786470930SIngo Molnar return -1; 16886470930SIngo Molnar value = parse_value(); 16986470930SIngo Molnar if (!value) 17086470930SIngo Molnar return -1; 17186470930SIngo Molnar } 17286470930SIngo Molnar return fn(name, value, data); 17386470930SIngo Molnar } 17486470930SIngo Molnar 17586470930SIngo Molnar static int get_extended_base_var(char *name, int baselen, int c) 17686470930SIngo Molnar { 17786470930SIngo Molnar do { 17886470930SIngo Molnar if (c == '\n') 17986470930SIngo Molnar return -1; 18086470930SIngo Molnar c = get_next_char(); 18186470930SIngo Molnar } while (isspace(c)); 18286470930SIngo Molnar 18386470930SIngo Molnar /* We require the format to be '[base "extension"]' */ 18486470930SIngo Molnar if (c != '"') 18586470930SIngo Molnar return -1; 18686470930SIngo Molnar name[baselen++] = '.'; 18786470930SIngo Molnar 18886470930SIngo Molnar for (;;) { 18983a0944fSIngo Molnar int ch = get_next_char(); 19083a0944fSIngo Molnar 19183a0944fSIngo Molnar if (ch == '\n') 19286470930SIngo Molnar return -1; 19383a0944fSIngo Molnar if (ch == '"') 19486470930SIngo Molnar break; 19583a0944fSIngo Molnar if (ch == '\\') { 19683a0944fSIngo Molnar ch = get_next_char(); 19783a0944fSIngo Molnar if (ch == '\n') 19886470930SIngo Molnar return -1; 19986470930SIngo Molnar } 20083a0944fSIngo Molnar name[baselen++] = ch; 20186470930SIngo Molnar if (baselen > MAXNAME / 2) 20286470930SIngo Molnar return -1; 20386470930SIngo Molnar } 20486470930SIngo Molnar 20586470930SIngo Molnar /* Final ']' */ 20686470930SIngo Molnar if (get_next_char() != ']') 20786470930SIngo Molnar return -1; 20886470930SIngo Molnar return baselen; 20986470930SIngo Molnar } 21086470930SIngo Molnar 21186470930SIngo Molnar static int get_base_var(char *name) 21286470930SIngo Molnar { 21386470930SIngo Molnar int baselen = 0; 21486470930SIngo Molnar 21586470930SIngo Molnar for (;;) { 21686470930SIngo Molnar int c = get_next_char(); 21786470930SIngo Molnar if (config_file_eof) 21886470930SIngo Molnar return -1; 21986470930SIngo Molnar if (c == ']') 22086470930SIngo Molnar return baselen; 22186470930SIngo Molnar if (isspace(c)) 22286470930SIngo Molnar return get_extended_base_var(name, baselen, c); 22386470930SIngo Molnar if (!iskeychar(c) && c != '.') 22486470930SIngo Molnar return -1; 22586470930SIngo Molnar if (baselen > MAXNAME / 2) 22686470930SIngo Molnar return -1; 22786470930SIngo Molnar name[baselen++] = tolower(c); 22886470930SIngo Molnar } 22986470930SIngo Molnar } 23086470930SIngo Molnar 23186470930SIngo Molnar static int perf_parse_file(config_fn_t fn, void *data) 23286470930SIngo Molnar { 23386470930SIngo Molnar int comment = 0; 23486470930SIngo Molnar int baselen = 0; 23586470930SIngo Molnar static char var[MAXNAME]; 23686470930SIngo Molnar 23786470930SIngo Molnar /* U+FEFF Byte Order Mark in UTF8 */ 23886470930SIngo Molnar static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; 23986470930SIngo Molnar const unsigned char *bomptr = utf8_bom; 24086470930SIngo Molnar 24186470930SIngo Molnar for (;;) { 24249757c9cSJiri Olsa int line, c = get_next_char(); 24349757c9cSJiri Olsa 24486470930SIngo Molnar if (bomptr && *bomptr) { 24586470930SIngo Molnar /* We are at the file beginning; skip UTF8-encoded BOM 24686470930SIngo Molnar * if present. Sane editors won't put this in on their 24786470930SIngo Molnar * own, but e.g. Windows Notepad will do it happily. */ 24886470930SIngo Molnar if ((unsigned char) c == *bomptr) { 24986470930SIngo Molnar bomptr++; 25086470930SIngo Molnar continue; 25186470930SIngo Molnar } else { 25286470930SIngo Molnar /* Do not tolerate partial BOM. */ 25386470930SIngo Molnar if (bomptr != utf8_bom) 25486470930SIngo Molnar break; 25586470930SIngo Molnar /* No BOM at file beginning. Cool. */ 25686470930SIngo Molnar bomptr = NULL; 25786470930SIngo Molnar } 25886470930SIngo Molnar } 25986470930SIngo Molnar if (c == '\n') { 26086470930SIngo Molnar if (config_file_eof) 26186470930SIngo Molnar return 0; 26286470930SIngo Molnar comment = 0; 26386470930SIngo Molnar continue; 26486470930SIngo Molnar } 26586470930SIngo Molnar if (comment || isspace(c)) 26686470930SIngo Molnar continue; 26786470930SIngo Molnar if (c == '#' || c == ';') { 26886470930SIngo Molnar comment = 1; 26986470930SIngo Molnar continue; 27086470930SIngo Molnar } 27186470930SIngo Molnar if (c == '[') { 27286470930SIngo Molnar baselen = get_base_var(var); 27386470930SIngo Molnar if (baselen <= 0) 27486470930SIngo Molnar break; 27586470930SIngo Molnar var[baselen++] = '.'; 27686470930SIngo Molnar var[baselen] = 0; 27786470930SIngo Molnar continue; 27886470930SIngo Molnar } 27986470930SIngo Molnar if (!isalpha(c)) 28086470930SIngo Molnar break; 28186470930SIngo Molnar var[baselen] = tolower(c); 28249757c9cSJiri Olsa 28349757c9cSJiri Olsa /* 28449757c9cSJiri Olsa * The get_value function might or might not reach the '\n', 28549757c9cSJiri Olsa * so saving the current line number for error reporting. 28649757c9cSJiri Olsa */ 28749757c9cSJiri Olsa line = config_linenr; 28849757c9cSJiri Olsa if (get_value(fn, data, var, baselen+1) < 0) { 28949757c9cSJiri Olsa config_linenr = line; 29086470930SIngo Molnar break; 29186470930SIngo Molnar } 29249757c9cSJiri Olsa } 29378f71c99STaeung Song pr_err("bad config file line %d in %s\n", config_linenr, config_file_name); 29478f71c99STaeung Song return -1; 29586470930SIngo Molnar } 29686470930SIngo Molnar 29786470930SIngo Molnar static int parse_unit_factor(const char *end, unsigned long *val) 29886470930SIngo Molnar { 29986470930SIngo Molnar if (!*end) 30086470930SIngo Molnar return 1; 30186470930SIngo Molnar else if (!strcasecmp(end, "k")) { 30286470930SIngo Molnar *val *= 1024; 30386470930SIngo Molnar return 1; 30486470930SIngo Molnar } 30586470930SIngo Molnar else if (!strcasecmp(end, "m")) { 30686470930SIngo Molnar *val *= 1024 * 1024; 30786470930SIngo Molnar return 1; 30886470930SIngo Molnar } 30986470930SIngo Molnar else if (!strcasecmp(end, "g")) { 31086470930SIngo Molnar *val *= 1024 * 1024 * 1024; 31186470930SIngo Molnar return 1; 31286470930SIngo Molnar } 31386470930SIngo Molnar return 0; 31486470930SIngo Molnar } 31586470930SIngo Molnar 31694c0655fSJiri Olsa static int perf_parse_llong(const char *value, long long *ret) 31794c0655fSJiri Olsa { 31894c0655fSJiri Olsa if (value && *value) { 31994c0655fSJiri Olsa char *end; 32094c0655fSJiri Olsa long long val = strtoll(value, &end, 0); 32194c0655fSJiri Olsa unsigned long factor = 1; 32294c0655fSJiri Olsa 32394c0655fSJiri Olsa if (!parse_unit_factor(end, &factor)) 32494c0655fSJiri Olsa return 0; 32594c0655fSJiri Olsa *ret = val * factor; 32694c0655fSJiri Olsa return 1; 32794c0655fSJiri Olsa } 32894c0655fSJiri Olsa return 0; 32994c0655fSJiri Olsa } 33094c0655fSJiri Olsa 33186470930SIngo Molnar static int perf_parse_long(const char *value, long *ret) 33286470930SIngo Molnar { 33386470930SIngo Molnar if (value && *value) { 33486470930SIngo Molnar char *end; 33586470930SIngo Molnar long val = strtol(value, &end, 0); 33686470930SIngo Molnar unsigned long factor = 1; 33786470930SIngo Molnar if (!parse_unit_factor(end, &factor)) 33886470930SIngo Molnar return 0; 33986470930SIngo Molnar *ret = val * factor; 34086470930SIngo Molnar return 1; 34186470930SIngo Molnar } 34286470930SIngo Molnar return 0; 34386470930SIngo Molnar } 34486470930SIngo Molnar 34525ce4bb8SArnaldo Carvalho de Melo static void bad_config(const char *name) 34686470930SIngo Molnar { 34786470930SIngo Molnar if (config_file_name) 34825ce4bb8SArnaldo Carvalho de Melo pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name); 34925ce4bb8SArnaldo Carvalho de Melo else 35025ce4bb8SArnaldo Carvalho de Melo pr_warning("bad config value for '%s', ignoring...\n", name); 35186470930SIngo Molnar } 35286470930SIngo Molnar 35325ce4bb8SArnaldo Carvalho de Melo int perf_config_u64(u64 *dest, const char *name, const char *value) 35494c0655fSJiri Olsa { 35594c0655fSJiri Olsa long long ret = 0; 35694c0655fSJiri Olsa 35725ce4bb8SArnaldo Carvalho de Melo if (!perf_parse_llong(value, &ret)) { 35825ce4bb8SArnaldo Carvalho de Melo bad_config(name); 35925ce4bb8SArnaldo Carvalho de Melo return -1; 36094c0655fSJiri Olsa } 36194c0655fSJiri Olsa 36225ce4bb8SArnaldo Carvalho de Melo *dest = ret; 36325ce4bb8SArnaldo Carvalho de Melo return 0; 36425ce4bb8SArnaldo Carvalho de Melo } 36525ce4bb8SArnaldo Carvalho de Melo 36625ce4bb8SArnaldo Carvalho de Melo int perf_config_int(int *dest, const char *name, const char *value) 36786470930SIngo Molnar { 36886470930SIngo Molnar long ret = 0; 36925ce4bb8SArnaldo Carvalho de Melo if (!perf_parse_long(value, &ret)) { 37025ce4bb8SArnaldo Carvalho de Melo bad_config(name); 37125ce4bb8SArnaldo Carvalho de Melo return -1; 37225ce4bb8SArnaldo Carvalho de Melo } 37325ce4bb8SArnaldo Carvalho de Melo *dest = ret; 37425ce4bb8SArnaldo Carvalho de Melo return 0; 37586470930SIngo Molnar } 37686470930SIngo Molnar 377a41794cdSArnaldo Carvalho de Melo static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool) 37886470930SIngo Molnar { 37925ce4bb8SArnaldo Carvalho de Melo int ret; 38025ce4bb8SArnaldo Carvalho de Melo 38186470930SIngo Molnar *is_bool = 1; 38286470930SIngo Molnar if (!value) 38386470930SIngo Molnar return 1; 38486470930SIngo Molnar if (!*value) 38586470930SIngo Molnar return 0; 38686470930SIngo Molnar if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on")) 38786470930SIngo Molnar return 1; 38886470930SIngo Molnar if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off")) 38986470930SIngo Molnar return 0; 39086470930SIngo Molnar *is_bool = 0; 39125ce4bb8SArnaldo Carvalho de Melo return perf_config_int(&ret, name, value) < 0 ? -1 : ret; 39286470930SIngo Molnar } 39386470930SIngo Molnar 39486470930SIngo Molnar int perf_config_bool(const char *name, const char *value) 39586470930SIngo Molnar { 39686470930SIngo Molnar int discard; 39786470930SIngo Molnar return !!perf_config_bool_or_int(name, value, &discard); 39886470930SIngo Molnar } 39986470930SIngo Molnar 400814b3f51SArnaldo Carvalho de Melo static const char *perf_config_dirname(const char *name, const char *value) 40145de34bbSStephane Eranian { 40245de34bbSStephane Eranian if (!name) 40345de34bbSStephane Eranian return NULL; 40445de34bbSStephane Eranian return value; 40545de34bbSStephane Eranian } 40645de34bbSStephane Eranian 4079cb5987cSTaeung Song static int perf_buildid_config(const char *var, const char *value) 4089cb5987cSTaeung Song { 4099cb5987cSTaeung Song /* same dir for all commands */ 4109cb5987cSTaeung Song if (!strcmp(var, "buildid.dir")) { 411d8e28654SVinson Lee const char *dir = perf_config_dirname(var, value); 4129cb5987cSTaeung Song 413ecc4c561SArnaldo Carvalho de Melo if (!dir) { 414ecc4c561SArnaldo Carvalho de Melo pr_err("Invalid buildid directory!\n"); 4159cb5987cSTaeung Song return -1; 416ecc4c561SArnaldo Carvalho de Melo } 417d8e28654SVinson Lee strncpy(buildid_dir, dir, MAXPATHLEN-1); 4189cb5987cSTaeung Song buildid_dir[MAXPATHLEN-1] = '\0'; 4199cb5987cSTaeung Song } 4209cb5987cSTaeung Song 4219cb5987cSTaeung Song return 0; 4229cb5987cSTaeung Song } 4239cb5987cSTaeung Song 4241d037ca1SIrina Tirdea static int perf_default_core_config(const char *var __maybe_unused, 4251d037ca1SIrina Tirdea const char *value __maybe_unused) 42686470930SIngo Molnar { 4273fcb10e4SMark Drayton if (!strcmp(var, "core.proc-map-timeout")) 4283fcb10e4SMark Drayton proc_map_timeout = strtoul(value, NULL, 10); 4293fcb10e4SMark Drayton 430395cf969SPaul Bolle /* Add other config variables here. */ 43186470930SIngo Molnar return 0; 43286470930SIngo Molnar } 43386470930SIngo Molnar 434c8302367SJiri Olsa static int perf_ui_config(const char *var, const char *value) 435c8302367SJiri Olsa { 436c8302367SJiri Olsa /* Add other config variables here. */ 437ecc4c561SArnaldo Carvalho de Melo if (!strcmp(var, "ui.show-headers")) 438c8302367SJiri Olsa symbol_conf.show_hist_headers = perf_config_bool(var, value); 439ecc4c561SArnaldo Carvalho de Melo 440c8302367SJiri Olsa return 0; 441c8302367SJiri Olsa } 442c8302367SJiri Olsa 4431d037ca1SIrina Tirdea int perf_default_config(const char *var, const char *value, 4441d037ca1SIrina Tirdea void *dummy __maybe_unused) 44586470930SIngo Molnar { 4468e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "core.")) 44786470930SIngo Molnar return perf_default_core_config(var, value); 44886470930SIngo Molnar 4498e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "hist.")) 4500b93da17SNamhyung Kim return perf_hist_config(var, value); 4510b93da17SNamhyung Kim 4528e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "ui.")) 453c8302367SJiri Olsa return perf_ui_config(var, value); 454c8302367SJiri Olsa 4558e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "call-graph.")) 4562b9240caSNamhyung Kim return perf_callchain_config(var, value); 4572b9240caSNamhyung Kim 4588e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "llvm.")) 459aa61fd05SWang Nan return perf_llvm_config(var, value); 460aa61fd05SWang Nan 4618e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "buildid.")) 4629cb5987cSTaeung Song return perf_buildid_config(var, value); 4639cb5987cSTaeung Song 464395cf969SPaul Bolle /* Add other config variables here. */ 46586470930SIngo Molnar return 0; 46686470930SIngo Molnar } 46786470930SIngo Molnar 468a41794cdSArnaldo Carvalho de Melo static int perf_config_from_file(config_fn_t fn, const char *filename, void *data) 46986470930SIngo Molnar { 47086470930SIngo Molnar int ret; 47186470930SIngo Molnar FILE *f = fopen(filename, "r"); 47286470930SIngo Molnar 47386470930SIngo Molnar ret = -1; 47486470930SIngo Molnar if (f) { 47586470930SIngo Molnar config_file = f; 47686470930SIngo Molnar config_file_name = filename; 47786470930SIngo Molnar config_linenr = 1; 47886470930SIngo Molnar config_file_eof = 0; 47986470930SIngo Molnar ret = perf_parse_file(fn, data); 48086470930SIngo Molnar fclose(f); 48186470930SIngo Molnar config_file_name = NULL; 48286470930SIngo Molnar } 48386470930SIngo Molnar return ret; 48486470930SIngo Molnar } 48586470930SIngo Molnar 486c7ac2417STaeung Song const char *perf_etc_perfconfig(void) 48786470930SIngo Molnar { 48886470930SIngo Molnar static const char *system_wide; 48986470930SIngo Molnar if (!system_wide) 49086470930SIngo Molnar system_wide = system_path(ETC_PERFCONFIG); 49186470930SIngo Molnar return system_wide; 49286470930SIngo Molnar } 49386470930SIngo Molnar 49486470930SIngo Molnar static int perf_env_bool(const char *k, int def) 49586470930SIngo Molnar { 49686470930SIngo Molnar const char *v = getenv(k); 49786470930SIngo Molnar return v ? perf_config_bool(k, v) : def; 49886470930SIngo Molnar } 49986470930SIngo Molnar 500a41794cdSArnaldo Carvalho de Melo static int perf_config_system(void) 50186470930SIngo Molnar { 50286470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); 50386470930SIngo Molnar } 50486470930SIngo Molnar 505a41794cdSArnaldo Carvalho de Melo static int perf_config_global(void) 50686470930SIngo Molnar { 50786470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); 50886470930SIngo Molnar } 50986470930SIngo Molnar 51020105ca1STaeung Song static struct perf_config_section *find_section(struct list_head *sections, 51120105ca1STaeung Song const char *section_name) 51220105ca1STaeung Song { 51320105ca1STaeung Song struct perf_config_section *section; 51420105ca1STaeung Song 51520105ca1STaeung Song list_for_each_entry(section, sections, node) 51620105ca1STaeung Song if (!strcmp(section->name, section_name)) 51720105ca1STaeung Song return section; 51820105ca1STaeung Song 51920105ca1STaeung Song return NULL; 52020105ca1STaeung Song } 52120105ca1STaeung Song 52220105ca1STaeung Song static struct perf_config_item *find_config_item(const char *name, 52320105ca1STaeung Song struct perf_config_section *section) 52420105ca1STaeung Song { 52520105ca1STaeung Song struct perf_config_item *item; 52620105ca1STaeung Song 52720105ca1STaeung Song list_for_each_entry(item, §ion->items, node) 52820105ca1STaeung Song if (!strcmp(item->name, name)) 52920105ca1STaeung Song return item; 53020105ca1STaeung Song 53120105ca1STaeung Song return NULL; 53220105ca1STaeung Song } 53320105ca1STaeung Song 53420105ca1STaeung Song static struct perf_config_section *add_section(struct list_head *sections, 53520105ca1STaeung Song const char *section_name) 53620105ca1STaeung Song { 53720105ca1STaeung Song struct perf_config_section *section = zalloc(sizeof(*section)); 53820105ca1STaeung Song 53920105ca1STaeung Song if (!section) 54020105ca1STaeung Song return NULL; 54120105ca1STaeung Song 54220105ca1STaeung Song INIT_LIST_HEAD(§ion->items); 54320105ca1STaeung Song section->name = strdup(section_name); 54420105ca1STaeung Song if (!section->name) { 54520105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 54620105ca1STaeung Song free(section); 54720105ca1STaeung Song return NULL; 54820105ca1STaeung Song } 54920105ca1STaeung Song 55020105ca1STaeung Song list_add_tail(§ion->node, sections); 55120105ca1STaeung Song return section; 55220105ca1STaeung Song } 55320105ca1STaeung Song 55420105ca1STaeung Song static struct perf_config_item *add_config_item(struct perf_config_section *section, 55520105ca1STaeung Song const char *name) 55620105ca1STaeung Song { 55720105ca1STaeung Song struct perf_config_item *item = zalloc(sizeof(*item)); 55820105ca1STaeung Song 55920105ca1STaeung Song if (!item) 56020105ca1STaeung Song return NULL; 56120105ca1STaeung Song 56220105ca1STaeung Song item->name = strdup(name); 56320105ca1STaeung Song if (!item->name) { 56420105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 56520105ca1STaeung Song free(item); 56620105ca1STaeung Song return NULL; 56720105ca1STaeung Song } 56820105ca1STaeung Song 56920105ca1STaeung Song list_add_tail(&item->node, §ion->items); 57020105ca1STaeung Song return item; 57120105ca1STaeung Song } 57220105ca1STaeung Song 57320105ca1STaeung Song static int set_value(struct perf_config_item *item, const char *value) 57420105ca1STaeung Song { 57520105ca1STaeung Song char *val = strdup(value); 57620105ca1STaeung Song 57720105ca1STaeung Song if (!val) 57820105ca1STaeung Song return -1; 57920105ca1STaeung Song 58020105ca1STaeung Song zfree(&item->value); 58120105ca1STaeung Song item->value = val; 58220105ca1STaeung Song return 0; 58320105ca1STaeung Song } 58420105ca1STaeung Song 58520105ca1STaeung Song static int collect_config(const char *var, const char *value, 58620105ca1STaeung Song void *perf_config_set) 58720105ca1STaeung Song { 58820105ca1STaeung Song int ret = -1; 58920105ca1STaeung Song char *ptr, *key; 59020105ca1STaeung Song char *section_name, *name; 59120105ca1STaeung Song struct perf_config_section *section = NULL; 59220105ca1STaeung Song struct perf_config_item *item = NULL; 59320105ca1STaeung Song struct perf_config_set *set = perf_config_set; 5947db91f25STaeung Song struct list_head *sections; 59520105ca1STaeung Song 5967db91f25STaeung Song if (set == NULL) 5977db91f25STaeung Song return -1; 5987db91f25STaeung Song 5997db91f25STaeung Song sections = &set->sections; 60020105ca1STaeung Song key = ptr = strdup(var); 60120105ca1STaeung Song if (!key) { 60220105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 60320105ca1STaeung Song return -1; 60420105ca1STaeung Song } 60520105ca1STaeung Song 60620105ca1STaeung Song section_name = strsep(&ptr, "."); 60720105ca1STaeung Song name = ptr; 60820105ca1STaeung Song if (name == NULL || value == NULL) 60920105ca1STaeung Song goto out_free; 61020105ca1STaeung Song 61120105ca1STaeung Song section = find_section(sections, section_name); 61220105ca1STaeung Song if (!section) { 61320105ca1STaeung Song section = add_section(sections, section_name); 61420105ca1STaeung Song if (!section) 61520105ca1STaeung Song goto out_free; 61620105ca1STaeung Song } 61720105ca1STaeung Song 61820105ca1STaeung Song item = find_config_item(name, section); 61920105ca1STaeung Song if (!item) { 62020105ca1STaeung Song item = add_config_item(section, name); 62120105ca1STaeung Song if (!item) 62220105ca1STaeung Song goto out_free; 62320105ca1STaeung Song } 62420105ca1STaeung Song 62508d090cfSTaeung Song /* perf_config_set can contain both user and system config items. 62608d090cfSTaeung Song * So we should know where each value is from. 62708d090cfSTaeung Song * The classification would be needed when a particular config file 62808d090cfSTaeung Song * is overwrited by setting feature i.e. set_config(). 62908d090cfSTaeung Song */ 63008d090cfSTaeung Song if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) { 63108d090cfSTaeung Song section->from_system_config = true; 63208d090cfSTaeung Song item->from_system_config = true; 63308d090cfSTaeung Song } else { 63408d090cfSTaeung Song section->from_system_config = false; 63508d090cfSTaeung Song item->from_system_config = false; 63608d090cfSTaeung Song } 63708d090cfSTaeung Song 63820105ca1STaeung Song ret = set_value(item, value); 63920105ca1STaeung Song 64020105ca1STaeung Song out_free: 64120105ca1STaeung Song free(key); 64254569ba4SChangbin Du return ret; 64320105ca1STaeung Song } 64420105ca1STaeung Song 64508d090cfSTaeung Song int perf_config_set__collect(struct perf_config_set *set, const char *file_name, 646c6fc018aSTaeung Song const char *var, const char *value) 647c6fc018aSTaeung Song { 64808d090cfSTaeung Song config_file_name = file_name; 649c6fc018aSTaeung Song return collect_config(var, value, set); 650c6fc018aSTaeung Song } 651c6fc018aSTaeung Song 6528beeb00fSTaeung Song static int perf_config_set__init(struct perf_config_set *set) 6538beeb00fSTaeung Song { 6548beeb00fSTaeung Song int ret = -1; 6558beeb00fSTaeung Song const char *home = NULL; 6563e00cbe8SJiri Olsa char *user_config; 6573e00cbe8SJiri Olsa struct stat st; 6588beeb00fSTaeung Song 6598beeb00fSTaeung Song /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ 6608beeb00fSTaeung Song if (config_exclusive_filename) 6618beeb00fSTaeung Song return perf_config_from_file(collect_config, config_exclusive_filename, set); 6628beeb00fSTaeung Song if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) { 6638beeb00fSTaeung Song if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0) 6648beeb00fSTaeung Song goto out; 6658beeb00fSTaeung Song } 6668beeb00fSTaeung Song 6678beeb00fSTaeung Song home = getenv("HOME"); 6688beeb00fSTaeung Song 6693e00cbe8SJiri Olsa /* 6703e00cbe8SJiri Olsa * Skip reading user config if: 6713e00cbe8SJiri Olsa * - there is no place to read it from (HOME) 6723e00cbe8SJiri Olsa * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) 6733e00cbe8SJiri Olsa */ 6743e00cbe8SJiri Olsa if (!home || !*home || !perf_config_global()) 6753e00cbe8SJiri Olsa return 0; 6763e00cbe8SJiri Olsa 6773e00cbe8SJiri Olsa user_config = strdup(mkpath("%s/.perfconfig", home)); 6788beeb00fSTaeung Song if (user_config == NULL) { 6794cf134e7SArnaldo Carvalho de Melo pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home); 6808beeb00fSTaeung Song goto out; 6818beeb00fSTaeung Song } 6828beeb00fSTaeung Song 683afc45cf5SArnaldo Carvalho de Melo if (stat(user_config, &st) < 0) { 684afc45cf5SArnaldo Carvalho de Melo if (errno == ENOENT) 685afc45cf5SArnaldo Carvalho de Melo ret = 0; 6868beeb00fSTaeung Song goto out_free; 687afc45cf5SArnaldo Carvalho de Melo } 688afc45cf5SArnaldo Carvalho de Melo 689afc45cf5SArnaldo Carvalho de Melo ret = 0; 6908beeb00fSTaeung Song 6918beeb00fSTaeung Song if (st.st_uid && (st.st_uid != geteuid())) { 6924cf134e7SArnaldo Carvalho de Melo pr_warning("File %s not owned by current user or root, ignoring it.", user_config); 6938beeb00fSTaeung Song goto out_free; 6948beeb00fSTaeung Song } 6958beeb00fSTaeung Song 696afc45cf5SArnaldo Carvalho de Melo if (st.st_size) 6978beeb00fSTaeung Song ret = perf_config_from_file(collect_config, user_config, set); 6983e00cbe8SJiri Olsa 6998beeb00fSTaeung Song out_free: 7008beeb00fSTaeung Song free(user_config); 7018beeb00fSTaeung Song out: 7028beeb00fSTaeung Song return ret; 7038beeb00fSTaeung Song } 7048beeb00fSTaeung Song 70520105ca1STaeung Song struct perf_config_set *perf_config_set__new(void) 70620105ca1STaeung Song { 70720105ca1STaeung Song struct perf_config_set *set = zalloc(sizeof(*set)); 70820105ca1STaeung Song 70920105ca1STaeung Song if (set) { 71020105ca1STaeung Song INIT_LIST_HEAD(&set->sections); 71155421b4fSTaeung Song perf_config_set__init(set); 71220105ca1STaeung Song } 71320105ca1STaeung Song 71420105ca1STaeung Song return set; 71520105ca1STaeung Song } 71620105ca1STaeung Song 717d01bd1acSArnaldo Carvalho de Melo static int perf_config__init(void) 718d01bd1acSArnaldo Carvalho de Melo { 719d01bd1acSArnaldo Carvalho de Melo if (config_set == NULL) 720d01bd1acSArnaldo Carvalho de Melo config_set = perf_config_set__new(); 721d01bd1acSArnaldo Carvalho de Melo 722d01bd1acSArnaldo Carvalho de Melo return config_set == NULL; 723d01bd1acSArnaldo Carvalho de Melo } 724d01bd1acSArnaldo Carvalho de Melo 7258a0a9c7eSTaeung Song int perf_config(config_fn_t fn, void *data) 7268a0a9c7eSTaeung Song { 7278a0a9c7eSTaeung Song int ret = 0; 7288a0a9c7eSTaeung Song char key[BUFSIZ]; 7298a0a9c7eSTaeung Song struct perf_config_section *section; 7308a0a9c7eSTaeung Song struct perf_config_item *item; 7318a0a9c7eSTaeung Song 732d01bd1acSArnaldo Carvalho de Melo if (config_set == NULL && perf_config__init()) 7338a0a9c7eSTaeung Song return -1; 7348a0a9c7eSTaeung Song 7358a0a9c7eSTaeung Song perf_config_set__for_each_entry(config_set, section, item) { 7368a0a9c7eSTaeung Song char *value = item->value; 7378a0a9c7eSTaeung Song 7388a0a9c7eSTaeung Song if (value) { 7398a0a9c7eSTaeung Song scnprintf(key, sizeof(key), "%s.%s", 7408a0a9c7eSTaeung Song section->name, item->name); 7418a0a9c7eSTaeung Song ret = fn(key, value, data); 7428a0a9c7eSTaeung Song if (ret < 0) { 7438a0a9c7eSTaeung Song pr_err("Error: wrong config key-value pair %s=%s\n", 7448a0a9c7eSTaeung Song key, value); 74522d46219SArnaldo Carvalho de Melo /* 74622d46219SArnaldo Carvalho de Melo * Can't be just a 'break', as perf_config_set__for_each_entry() 74722d46219SArnaldo Carvalho de Melo * expands to two nested for() loops. 74822d46219SArnaldo Carvalho de Melo */ 74922d46219SArnaldo Carvalho de Melo goto out; 7508a0a9c7eSTaeung Song } 7518a0a9c7eSTaeung Song } 7528a0a9c7eSTaeung Song } 75322d46219SArnaldo Carvalho de Melo out: 7548a0a9c7eSTaeung Song return ret; 7558a0a9c7eSTaeung Song } 7568a0a9c7eSTaeung Song 7578a0a9c7eSTaeung Song void perf_config__exit(void) 7588a0a9c7eSTaeung Song { 7598a0a9c7eSTaeung Song perf_config_set__delete(config_set); 7608a0a9c7eSTaeung Song config_set = NULL; 7618a0a9c7eSTaeung Song } 7628a0a9c7eSTaeung Song 7638a0a9c7eSTaeung Song void perf_config__refresh(void) 7648a0a9c7eSTaeung Song { 7658a0a9c7eSTaeung Song perf_config__exit(); 7668a0a9c7eSTaeung Song perf_config__init(); 7678a0a9c7eSTaeung Song } 7688a0a9c7eSTaeung Song 76920105ca1STaeung Song static void perf_config_item__delete(struct perf_config_item *item) 77020105ca1STaeung Song { 77120105ca1STaeung Song zfree(&item->name); 77220105ca1STaeung Song zfree(&item->value); 77320105ca1STaeung Song free(item); 77420105ca1STaeung Song } 77520105ca1STaeung Song 77620105ca1STaeung Song static void perf_config_section__purge(struct perf_config_section *section) 77720105ca1STaeung Song { 77820105ca1STaeung Song struct perf_config_item *item, *tmp; 77920105ca1STaeung Song 78020105ca1STaeung Song list_for_each_entry_safe(item, tmp, §ion->items, node) { 78120105ca1STaeung Song list_del_init(&item->node); 78220105ca1STaeung Song perf_config_item__delete(item); 78320105ca1STaeung Song } 78420105ca1STaeung Song } 78520105ca1STaeung Song 78620105ca1STaeung Song static void perf_config_section__delete(struct perf_config_section *section) 78720105ca1STaeung Song { 78820105ca1STaeung Song perf_config_section__purge(section); 78920105ca1STaeung Song zfree(§ion->name); 79020105ca1STaeung Song free(section); 79120105ca1STaeung Song } 79220105ca1STaeung Song 79320105ca1STaeung Song static void perf_config_set__purge(struct perf_config_set *set) 79420105ca1STaeung Song { 79520105ca1STaeung Song struct perf_config_section *section, *tmp; 79620105ca1STaeung Song 79720105ca1STaeung Song list_for_each_entry_safe(section, tmp, &set->sections, node) { 79820105ca1STaeung Song list_del_init(§ion->node); 79920105ca1STaeung Song perf_config_section__delete(section); 80020105ca1STaeung Song } 80120105ca1STaeung Song } 80220105ca1STaeung Song 80320105ca1STaeung Song void perf_config_set__delete(struct perf_config_set *set) 80420105ca1STaeung Song { 805826424ccSTaeung Song if (set == NULL) 806826424ccSTaeung Song return; 807826424ccSTaeung Song 80820105ca1STaeung Song perf_config_set__purge(set); 80920105ca1STaeung Song free(set); 81020105ca1STaeung Song } 81120105ca1STaeung Song 81286470930SIngo Molnar /* 81386470930SIngo Molnar * Call this to report error for your variable that should not 81486470930SIngo Molnar * get a boolean value (i.e. "[my] var" means "true"). 81586470930SIngo Molnar */ 81686470930SIngo Molnar int config_error_nonbool(const char *var) 81786470930SIngo Molnar { 81862d94b00SArnaldo Carvalho de Melo pr_err("Missing value for '%s'", var); 81962d94b00SArnaldo Carvalho de Melo return -1; 82086470930SIngo Molnar } 82145de34bbSStephane Eranian 82299ce8e9fSJiri Olsa void set_buildid_dir(const char *dir) 82345de34bbSStephane Eranian { 82499ce8e9fSJiri Olsa if (dir) 82575c375c0SSihyeon Jang scnprintf(buildid_dir, MAXPATHLEN, "%s", dir); 82645de34bbSStephane Eranian 82745de34bbSStephane Eranian /* default to $HOME/.debug */ 82845de34bbSStephane Eranian if (buildid_dir[0] == '\0') { 82937194f44STaeung Song char *home = getenv("HOME"); 83037194f44STaeung Song 83137194f44STaeung Song if (home) { 83275c375c0SSihyeon Jang snprintf(buildid_dir, MAXPATHLEN, "%s/%s", 83337194f44STaeung Song home, DEBUG_CACHE_DIR); 83445de34bbSStephane Eranian } else { 83545de34bbSStephane Eranian strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1); 83645de34bbSStephane Eranian } 83745de34bbSStephane Eranian buildid_dir[MAXPATHLEN-1] = '\0'; 83845de34bbSStephane Eranian } 83945de34bbSStephane Eranian /* for communicating with external commands */ 84045de34bbSStephane Eranian setenv("PERF_BUILDID_DIR", buildid_dir, 1); 84145de34bbSStephane Eranian } 842