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 */ 20*d778a778SPaul A. Clarke #include "util/stat.h" /* perf_stat__set_big_num */ 214cb3c6d5SArnaldo Carvalho de Melo #include "build-id.h" 224cb3c6d5SArnaldo Carvalho de Melo #include "debug.h" 2320105ca1STaeung Song #include "config.h" 24b4209025SArnaldo Carvalho de Melo #include "debug.h" 257a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h> 267a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 27f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 287a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h> 298e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h> 307f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 313052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h> 323d689ed6SArnaldo Carvalho de Melo 3386470930SIngo Molnar #define MAXNAME (256) 3486470930SIngo Molnar 3545de34bbSStephane Eranian #define DEBUG_CACHE_DIR ".debug" 3645de34bbSStephane Eranian 3745de34bbSStephane Eranian 3845de34bbSStephane Eranian char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ 3945de34bbSStephane Eranian 4086470930SIngo Molnar static FILE *config_file; 4186470930SIngo Molnar static const char *config_file_name; 4286470930SIngo Molnar static int config_linenr; 4386470930SIngo Molnar static int config_file_eof; 448a0a9c7eSTaeung Song static struct perf_config_set *config_set; 4586470930SIngo Molnar 46c7ac2417STaeung Song const char *config_exclusive_filename; 4786470930SIngo Molnar 4886470930SIngo Molnar static int get_next_char(void) 4986470930SIngo Molnar { 5086470930SIngo Molnar int c; 5186470930SIngo Molnar FILE *f; 5286470930SIngo Molnar 5386470930SIngo Molnar c = '\n'; 5486470930SIngo Molnar if ((f = config_file) != NULL) { 5586470930SIngo Molnar c = fgetc(f); 5686470930SIngo Molnar if (c == '\r') { 5786470930SIngo Molnar /* DOS like systems */ 5886470930SIngo Molnar c = fgetc(f); 5986470930SIngo Molnar if (c != '\n') { 6086470930SIngo Molnar ungetc(c, f); 6186470930SIngo Molnar c = '\r'; 6286470930SIngo Molnar } 6386470930SIngo Molnar } 6486470930SIngo Molnar if (c == '\n') 6586470930SIngo Molnar config_linenr++; 6686470930SIngo Molnar if (c == EOF) { 6786470930SIngo Molnar config_file_eof = 1; 6886470930SIngo Molnar c = '\n'; 6986470930SIngo Molnar } 7086470930SIngo Molnar } 7186470930SIngo Molnar return c; 7286470930SIngo Molnar } 7386470930SIngo Molnar 7486470930SIngo Molnar static char *parse_value(void) 7586470930SIngo Molnar { 7686470930SIngo Molnar static char value[1024]; 77f37a291cSIngo Molnar int quote = 0, comment = 0, space = 0; 78f37a291cSIngo Molnar size_t len = 0; 7986470930SIngo Molnar 8086470930SIngo Molnar for (;;) { 8186470930SIngo Molnar int c = get_next_char(); 82f37a291cSIngo Molnar 8386470930SIngo Molnar if (len >= sizeof(value) - 1) 8486470930SIngo Molnar return NULL; 8586470930SIngo Molnar if (c == '\n') { 8686470930SIngo Molnar if (quote) 8786470930SIngo Molnar return NULL; 8886470930SIngo Molnar value[len] = 0; 8986470930SIngo Molnar return value; 9086470930SIngo Molnar } 9186470930SIngo Molnar if (comment) 9286470930SIngo Molnar continue; 9386470930SIngo Molnar if (isspace(c) && !quote) { 9486470930SIngo Molnar space = 1; 9586470930SIngo Molnar continue; 9686470930SIngo Molnar } 9786470930SIngo Molnar if (!quote) { 9886470930SIngo Molnar if (c == ';' || c == '#') { 9986470930SIngo Molnar comment = 1; 10086470930SIngo Molnar continue; 10186470930SIngo Molnar } 10286470930SIngo Molnar } 10386470930SIngo Molnar if (space) { 10486470930SIngo Molnar if (len) 10586470930SIngo Molnar value[len++] = ' '; 10686470930SIngo Molnar space = 0; 10786470930SIngo Molnar } 10886470930SIngo Molnar if (c == '\\') { 10986470930SIngo Molnar c = get_next_char(); 11086470930SIngo Molnar switch (c) { 11186470930SIngo Molnar case '\n': 11286470930SIngo Molnar continue; 11386470930SIngo Molnar case 't': 11486470930SIngo Molnar c = '\t'; 11586470930SIngo Molnar break; 11686470930SIngo Molnar case 'b': 11786470930SIngo Molnar c = '\b'; 11886470930SIngo Molnar break; 11986470930SIngo Molnar case 'n': 12086470930SIngo Molnar c = '\n'; 12186470930SIngo Molnar break; 12286470930SIngo Molnar /* Some characters escape as themselves */ 12386470930SIngo Molnar case '\\': case '"': 12486470930SIngo Molnar break; 12586470930SIngo Molnar /* Reject unknown escape sequences */ 12686470930SIngo Molnar default: 12786470930SIngo Molnar return NULL; 12886470930SIngo Molnar } 12986470930SIngo Molnar value[len++] = c; 13086470930SIngo Molnar continue; 13186470930SIngo Molnar } 13286470930SIngo Molnar if (c == '"') { 13386470930SIngo Molnar quote = 1-quote; 13486470930SIngo Molnar continue; 13586470930SIngo Molnar } 13686470930SIngo Molnar value[len++] = c; 13786470930SIngo Molnar } 13886470930SIngo Molnar } 13986470930SIngo Molnar 14086470930SIngo Molnar static inline int iskeychar(int c) 14186470930SIngo Molnar { 1428dc7c651SArnaldo Carvalho de Melo return isalnum(c) || c == '-' || c == '_'; 14386470930SIngo Molnar } 14486470930SIngo Molnar 14586470930SIngo Molnar static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) 14686470930SIngo Molnar { 14786470930SIngo Molnar int c; 14886470930SIngo Molnar char *value; 14986470930SIngo Molnar 15086470930SIngo Molnar /* Get the full name */ 15186470930SIngo Molnar for (;;) { 15286470930SIngo Molnar c = get_next_char(); 15386470930SIngo Molnar if (config_file_eof) 15486470930SIngo Molnar break; 15586470930SIngo Molnar if (!iskeychar(c)) 15686470930SIngo Molnar break; 15745de34bbSStephane Eranian name[len++] = c; 15886470930SIngo Molnar if (len >= MAXNAME) 15986470930SIngo Molnar return -1; 16086470930SIngo Molnar } 16186470930SIngo Molnar name[len] = 0; 16286470930SIngo Molnar while (c == ' ' || c == '\t') 16386470930SIngo Molnar c = get_next_char(); 16486470930SIngo Molnar 16586470930SIngo Molnar value = NULL; 16686470930SIngo Molnar if (c != '\n') { 16786470930SIngo Molnar if (c != '=') 16886470930SIngo Molnar return -1; 16986470930SIngo Molnar value = parse_value(); 17086470930SIngo Molnar if (!value) 17186470930SIngo Molnar return -1; 17286470930SIngo Molnar } 17386470930SIngo Molnar return fn(name, value, data); 17486470930SIngo Molnar } 17586470930SIngo Molnar 17686470930SIngo Molnar static int get_extended_base_var(char *name, int baselen, int c) 17786470930SIngo Molnar { 17886470930SIngo Molnar do { 17986470930SIngo Molnar if (c == '\n') 18086470930SIngo Molnar return -1; 18186470930SIngo Molnar c = get_next_char(); 18286470930SIngo Molnar } while (isspace(c)); 18386470930SIngo Molnar 18486470930SIngo Molnar /* We require the format to be '[base "extension"]' */ 18586470930SIngo Molnar if (c != '"') 18686470930SIngo Molnar return -1; 18786470930SIngo Molnar name[baselen++] = '.'; 18886470930SIngo Molnar 18986470930SIngo Molnar for (;;) { 19083a0944fSIngo Molnar int ch = get_next_char(); 19183a0944fSIngo Molnar 19283a0944fSIngo Molnar if (ch == '\n') 19386470930SIngo Molnar return -1; 19483a0944fSIngo Molnar if (ch == '"') 19586470930SIngo Molnar break; 19683a0944fSIngo Molnar if (ch == '\\') { 19783a0944fSIngo Molnar ch = get_next_char(); 19883a0944fSIngo Molnar if (ch == '\n') 19986470930SIngo Molnar return -1; 20086470930SIngo Molnar } 20183a0944fSIngo Molnar name[baselen++] = ch; 20286470930SIngo Molnar if (baselen > MAXNAME / 2) 20386470930SIngo Molnar return -1; 20486470930SIngo Molnar } 20586470930SIngo Molnar 20686470930SIngo Molnar /* Final ']' */ 20786470930SIngo Molnar if (get_next_char() != ']') 20886470930SIngo Molnar return -1; 20986470930SIngo Molnar return baselen; 21086470930SIngo Molnar } 21186470930SIngo Molnar 21286470930SIngo Molnar static int get_base_var(char *name) 21386470930SIngo Molnar { 21486470930SIngo Molnar int baselen = 0; 21586470930SIngo Molnar 21686470930SIngo Molnar for (;;) { 21786470930SIngo Molnar int c = get_next_char(); 21886470930SIngo Molnar if (config_file_eof) 21986470930SIngo Molnar return -1; 22086470930SIngo Molnar if (c == ']') 22186470930SIngo Molnar return baselen; 22286470930SIngo Molnar if (isspace(c)) 22386470930SIngo Molnar return get_extended_base_var(name, baselen, c); 22486470930SIngo Molnar if (!iskeychar(c) && c != '.') 22586470930SIngo Molnar return -1; 22686470930SIngo Molnar if (baselen > MAXNAME / 2) 22786470930SIngo Molnar return -1; 22886470930SIngo Molnar name[baselen++] = tolower(c); 22986470930SIngo Molnar } 23086470930SIngo Molnar } 23186470930SIngo Molnar 23286470930SIngo Molnar static int perf_parse_file(config_fn_t fn, void *data) 23386470930SIngo Molnar { 23486470930SIngo Molnar int comment = 0; 23586470930SIngo Molnar int baselen = 0; 23686470930SIngo Molnar static char var[MAXNAME]; 23786470930SIngo Molnar 23886470930SIngo Molnar /* U+FEFF Byte Order Mark in UTF8 */ 23986470930SIngo Molnar static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; 24086470930SIngo Molnar const unsigned char *bomptr = utf8_bom; 24186470930SIngo Molnar 24286470930SIngo Molnar for (;;) { 24349757c9cSJiri Olsa int line, c = get_next_char(); 24449757c9cSJiri Olsa 24586470930SIngo Molnar if (bomptr && *bomptr) { 24686470930SIngo Molnar /* We are at the file beginning; skip UTF8-encoded BOM 24786470930SIngo Molnar * if present. Sane editors won't put this in on their 24886470930SIngo Molnar * own, but e.g. Windows Notepad will do it happily. */ 24986470930SIngo Molnar if ((unsigned char) c == *bomptr) { 25086470930SIngo Molnar bomptr++; 25186470930SIngo Molnar continue; 25286470930SIngo Molnar } else { 25386470930SIngo Molnar /* Do not tolerate partial BOM. */ 25486470930SIngo Molnar if (bomptr != utf8_bom) 25586470930SIngo Molnar break; 25686470930SIngo Molnar /* No BOM at file beginning. Cool. */ 25786470930SIngo Molnar bomptr = NULL; 25886470930SIngo Molnar } 25986470930SIngo Molnar } 26086470930SIngo Molnar if (c == '\n') { 26186470930SIngo Molnar if (config_file_eof) 26286470930SIngo Molnar return 0; 26386470930SIngo Molnar comment = 0; 26486470930SIngo Molnar continue; 26586470930SIngo Molnar } 26686470930SIngo Molnar if (comment || isspace(c)) 26786470930SIngo Molnar continue; 26886470930SIngo Molnar if (c == '#' || c == ';') { 26986470930SIngo Molnar comment = 1; 27086470930SIngo Molnar continue; 27186470930SIngo Molnar } 27286470930SIngo Molnar if (c == '[') { 27386470930SIngo Molnar baselen = get_base_var(var); 27486470930SIngo Molnar if (baselen <= 0) 27586470930SIngo Molnar break; 27686470930SIngo Molnar var[baselen++] = '.'; 27786470930SIngo Molnar var[baselen] = 0; 27886470930SIngo Molnar continue; 27986470930SIngo Molnar } 28086470930SIngo Molnar if (!isalpha(c)) 28186470930SIngo Molnar break; 28286470930SIngo Molnar var[baselen] = tolower(c); 28349757c9cSJiri Olsa 28449757c9cSJiri Olsa /* 28549757c9cSJiri Olsa * The get_value function might or might not reach the '\n', 28649757c9cSJiri Olsa * so saving the current line number for error reporting. 28749757c9cSJiri Olsa */ 28849757c9cSJiri Olsa line = config_linenr; 28949757c9cSJiri Olsa if (get_value(fn, data, var, baselen+1) < 0) { 29049757c9cSJiri Olsa config_linenr = line; 29186470930SIngo Molnar break; 29286470930SIngo Molnar } 29349757c9cSJiri Olsa } 29478f71c99STaeung Song pr_err("bad config file line %d in %s\n", config_linenr, config_file_name); 29578f71c99STaeung Song return -1; 29686470930SIngo Molnar } 29786470930SIngo Molnar 29886470930SIngo Molnar static int parse_unit_factor(const char *end, unsigned long *val) 29986470930SIngo Molnar { 30086470930SIngo Molnar if (!*end) 30186470930SIngo Molnar return 1; 30286470930SIngo Molnar else if (!strcasecmp(end, "k")) { 30386470930SIngo Molnar *val *= 1024; 30486470930SIngo Molnar return 1; 30586470930SIngo Molnar } 30686470930SIngo Molnar else if (!strcasecmp(end, "m")) { 30786470930SIngo Molnar *val *= 1024 * 1024; 30886470930SIngo Molnar return 1; 30986470930SIngo Molnar } 31086470930SIngo Molnar else if (!strcasecmp(end, "g")) { 31186470930SIngo Molnar *val *= 1024 * 1024 * 1024; 31286470930SIngo Molnar return 1; 31386470930SIngo Molnar } 31486470930SIngo Molnar return 0; 31586470930SIngo Molnar } 31686470930SIngo Molnar 31794c0655fSJiri Olsa static int perf_parse_llong(const char *value, long long *ret) 31894c0655fSJiri Olsa { 31994c0655fSJiri Olsa if (value && *value) { 32094c0655fSJiri Olsa char *end; 32194c0655fSJiri Olsa long long val = strtoll(value, &end, 0); 32294c0655fSJiri Olsa unsigned long factor = 1; 32394c0655fSJiri Olsa 32494c0655fSJiri Olsa if (!parse_unit_factor(end, &factor)) 32594c0655fSJiri Olsa return 0; 32694c0655fSJiri Olsa *ret = val * factor; 32794c0655fSJiri Olsa return 1; 32894c0655fSJiri Olsa } 32994c0655fSJiri Olsa return 0; 33094c0655fSJiri Olsa } 33194c0655fSJiri Olsa 33286470930SIngo Molnar static int perf_parse_long(const char *value, long *ret) 33386470930SIngo Molnar { 33486470930SIngo Molnar if (value && *value) { 33586470930SIngo Molnar char *end; 33686470930SIngo Molnar long val = strtol(value, &end, 0); 33786470930SIngo Molnar unsigned long factor = 1; 33886470930SIngo Molnar if (!parse_unit_factor(end, &factor)) 33986470930SIngo Molnar return 0; 34086470930SIngo Molnar *ret = val * factor; 34186470930SIngo Molnar return 1; 34286470930SIngo Molnar } 34386470930SIngo Molnar return 0; 34486470930SIngo Molnar } 34586470930SIngo Molnar 34625ce4bb8SArnaldo Carvalho de Melo static void bad_config(const char *name) 34786470930SIngo Molnar { 34886470930SIngo Molnar if (config_file_name) 34925ce4bb8SArnaldo Carvalho de Melo pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name); 35025ce4bb8SArnaldo Carvalho de Melo else 35125ce4bb8SArnaldo Carvalho de Melo pr_warning("bad config value for '%s', ignoring...\n", name); 35286470930SIngo Molnar } 35386470930SIngo Molnar 35425ce4bb8SArnaldo Carvalho de Melo int perf_config_u64(u64 *dest, const char *name, const char *value) 35594c0655fSJiri Olsa { 35694c0655fSJiri Olsa long long ret = 0; 35794c0655fSJiri Olsa 35825ce4bb8SArnaldo Carvalho de Melo if (!perf_parse_llong(value, &ret)) { 35925ce4bb8SArnaldo Carvalho de Melo bad_config(name); 36025ce4bb8SArnaldo Carvalho de Melo return -1; 36194c0655fSJiri Olsa } 36294c0655fSJiri Olsa 36325ce4bb8SArnaldo Carvalho de Melo *dest = ret; 36425ce4bb8SArnaldo Carvalho de Melo return 0; 36525ce4bb8SArnaldo Carvalho de Melo } 36625ce4bb8SArnaldo Carvalho de Melo 36725ce4bb8SArnaldo Carvalho de Melo int perf_config_int(int *dest, const char *name, const char *value) 36886470930SIngo Molnar { 36986470930SIngo Molnar long ret = 0; 37025ce4bb8SArnaldo Carvalho de Melo if (!perf_parse_long(value, &ret)) { 37125ce4bb8SArnaldo Carvalho de Melo bad_config(name); 37225ce4bb8SArnaldo Carvalho de Melo return -1; 37325ce4bb8SArnaldo Carvalho de Melo } 37425ce4bb8SArnaldo Carvalho de Melo *dest = ret; 37525ce4bb8SArnaldo Carvalho de Melo return 0; 37686470930SIngo Molnar } 37786470930SIngo Molnar 3787b43b697SRavi Bangoria int perf_config_u8(u8 *dest, const char *name, const char *value) 3797b43b697SRavi Bangoria { 3807b43b697SRavi Bangoria long ret = 0; 3817b43b697SRavi Bangoria 3827b43b697SRavi Bangoria if (!perf_parse_long(value, &ret)) { 3837b43b697SRavi Bangoria bad_config(name); 3847b43b697SRavi Bangoria return -1; 3857b43b697SRavi Bangoria } 3867b43b697SRavi Bangoria *dest = ret; 3877b43b697SRavi Bangoria return 0; 3887b43b697SRavi Bangoria } 3897b43b697SRavi Bangoria 390a41794cdSArnaldo Carvalho de Melo static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool) 39186470930SIngo Molnar { 39225ce4bb8SArnaldo Carvalho de Melo int ret; 39325ce4bb8SArnaldo Carvalho de Melo 39486470930SIngo Molnar *is_bool = 1; 39586470930SIngo Molnar if (!value) 39686470930SIngo Molnar return 1; 39786470930SIngo Molnar if (!*value) 39886470930SIngo Molnar return 0; 39986470930SIngo Molnar if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on")) 40086470930SIngo Molnar return 1; 40186470930SIngo Molnar if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off")) 40286470930SIngo Molnar return 0; 40386470930SIngo Molnar *is_bool = 0; 40425ce4bb8SArnaldo Carvalho de Melo return perf_config_int(&ret, name, value) < 0 ? -1 : ret; 40586470930SIngo Molnar } 40686470930SIngo Molnar 40786470930SIngo Molnar int perf_config_bool(const char *name, const char *value) 40886470930SIngo Molnar { 40986470930SIngo Molnar int discard; 41086470930SIngo Molnar return !!perf_config_bool_or_int(name, value, &discard); 41186470930SIngo Molnar } 41286470930SIngo Molnar 413814b3f51SArnaldo Carvalho de Melo static const char *perf_config_dirname(const char *name, const char *value) 41445de34bbSStephane Eranian { 41545de34bbSStephane Eranian if (!name) 41645de34bbSStephane Eranian return NULL; 41745de34bbSStephane Eranian return value; 41845de34bbSStephane Eranian } 41945de34bbSStephane Eranian 4209cb5987cSTaeung Song static int perf_buildid_config(const char *var, const char *value) 4219cb5987cSTaeung Song { 4229cb5987cSTaeung Song /* same dir for all commands */ 4239cb5987cSTaeung Song if (!strcmp(var, "buildid.dir")) { 424d8e28654SVinson Lee const char *dir = perf_config_dirname(var, value); 4259cb5987cSTaeung Song 426ecc4c561SArnaldo Carvalho de Melo if (!dir) { 427ecc4c561SArnaldo Carvalho de Melo pr_err("Invalid buildid directory!\n"); 4289cb5987cSTaeung Song return -1; 429ecc4c561SArnaldo Carvalho de Melo } 430d8e28654SVinson Lee strncpy(buildid_dir, dir, MAXPATHLEN-1); 4319cb5987cSTaeung Song buildid_dir[MAXPATHLEN-1] = '\0'; 4329cb5987cSTaeung Song } 4339cb5987cSTaeung Song 4349cb5987cSTaeung Song return 0; 4359cb5987cSTaeung Song } 4369cb5987cSTaeung Song 4371d037ca1SIrina Tirdea static int perf_default_core_config(const char *var __maybe_unused, 4381d037ca1SIrina Tirdea const char *value __maybe_unused) 43986470930SIngo Molnar { 4403fcb10e4SMark Drayton if (!strcmp(var, "core.proc-map-timeout")) 4413fcb10e4SMark Drayton proc_map_timeout = strtoul(value, NULL, 10); 4423fcb10e4SMark Drayton 443395cf969SPaul Bolle /* Add other config variables here. */ 44486470930SIngo Molnar return 0; 44586470930SIngo Molnar } 44686470930SIngo Molnar 447c8302367SJiri Olsa static int perf_ui_config(const char *var, const char *value) 448c8302367SJiri Olsa { 449c8302367SJiri Olsa /* Add other config variables here. */ 450ecc4c561SArnaldo Carvalho de Melo if (!strcmp(var, "ui.show-headers")) 451c8302367SJiri Olsa symbol_conf.show_hist_headers = perf_config_bool(var, value); 452ecc4c561SArnaldo Carvalho de Melo 453c8302367SJiri Olsa return 0; 454c8302367SJiri Olsa } 455c8302367SJiri Olsa 456*d778a778SPaul A. Clarke static int perf_stat_config(const char *var, const char *value) 457*d778a778SPaul A. Clarke { 458*d778a778SPaul A. Clarke if (!strcmp(var, "stat.big-num")) 459*d778a778SPaul A. Clarke perf_stat__set_big_num(perf_config_bool(var, value)); 460*d778a778SPaul A. Clarke 461*d778a778SPaul A. Clarke /* Add other config variables here. */ 462*d778a778SPaul A. Clarke return 0; 463*d778a778SPaul A. Clarke } 464*d778a778SPaul A. Clarke 4651d037ca1SIrina Tirdea int perf_default_config(const char *var, const char *value, 4661d037ca1SIrina Tirdea void *dummy __maybe_unused) 46786470930SIngo Molnar { 4688e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "core.")) 46986470930SIngo Molnar return perf_default_core_config(var, value); 47086470930SIngo Molnar 4718e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "hist.")) 4720b93da17SNamhyung Kim return perf_hist_config(var, value); 4730b93da17SNamhyung Kim 4748e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "ui.")) 475c8302367SJiri Olsa return perf_ui_config(var, value); 476c8302367SJiri Olsa 4778e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "call-graph.")) 4782b9240caSNamhyung Kim return perf_callchain_config(var, value); 4792b9240caSNamhyung Kim 4808e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "llvm.")) 481aa61fd05SWang Nan return perf_llvm_config(var, value); 482aa61fd05SWang Nan 4838e99b6d4SArnaldo Carvalho de Melo if (strstarts(var, "buildid.")) 4849cb5987cSTaeung Song return perf_buildid_config(var, value); 4859cb5987cSTaeung Song 486*d778a778SPaul A. Clarke if (strstarts(var, "stat.")) 487*d778a778SPaul A. Clarke return perf_stat_config(var, value); 488*d778a778SPaul A. Clarke 489395cf969SPaul Bolle /* Add other config variables here. */ 49086470930SIngo Molnar return 0; 49186470930SIngo Molnar } 49286470930SIngo Molnar 493a41794cdSArnaldo Carvalho de Melo static int perf_config_from_file(config_fn_t fn, const char *filename, void *data) 49486470930SIngo Molnar { 49586470930SIngo Molnar int ret; 49686470930SIngo Molnar FILE *f = fopen(filename, "r"); 49786470930SIngo Molnar 49886470930SIngo Molnar ret = -1; 49986470930SIngo Molnar if (f) { 50086470930SIngo Molnar config_file = f; 50186470930SIngo Molnar config_file_name = filename; 50286470930SIngo Molnar config_linenr = 1; 50386470930SIngo Molnar config_file_eof = 0; 50486470930SIngo Molnar ret = perf_parse_file(fn, data); 50586470930SIngo Molnar fclose(f); 50686470930SIngo Molnar config_file_name = NULL; 50786470930SIngo Molnar } 50886470930SIngo Molnar return ret; 50986470930SIngo Molnar } 51086470930SIngo Molnar 511c7ac2417STaeung Song const char *perf_etc_perfconfig(void) 51286470930SIngo Molnar { 51386470930SIngo Molnar static const char *system_wide; 51486470930SIngo Molnar if (!system_wide) 51586470930SIngo Molnar system_wide = system_path(ETC_PERFCONFIG); 51686470930SIngo Molnar return system_wide; 51786470930SIngo Molnar } 51886470930SIngo Molnar 51986470930SIngo Molnar static int perf_env_bool(const char *k, int def) 52086470930SIngo Molnar { 52186470930SIngo Molnar const char *v = getenv(k); 52286470930SIngo Molnar return v ? perf_config_bool(k, v) : def; 52386470930SIngo Molnar } 52486470930SIngo Molnar 525a41794cdSArnaldo Carvalho de Melo static int perf_config_system(void) 52686470930SIngo Molnar { 52786470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); 52886470930SIngo Molnar } 52986470930SIngo Molnar 530a41794cdSArnaldo Carvalho de Melo static int perf_config_global(void) 53186470930SIngo Molnar { 53286470930SIngo Molnar return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); 53386470930SIngo Molnar } 53486470930SIngo Molnar 53520105ca1STaeung Song static struct perf_config_section *find_section(struct list_head *sections, 53620105ca1STaeung Song const char *section_name) 53720105ca1STaeung Song { 53820105ca1STaeung Song struct perf_config_section *section; 53920105ca1STaeung Song 54020105ca1STaeung Song list_for_each_entry(section, sections, node) 54120105ca1STaeung Song if (!strcmp(section->name, section_name)) 54220105ca1STaeung Song return section; 54320105ca1STaeung Song 54420105ca1STaeung Song return NULL; 54520105ca1STaeung Song } 54620105ca1STaeung Song 54720105ca1STaeung Song static struct perf_config_item *find_config_item(const char *name, 54820105ca1STaeung Song struct perf_config_section *section) 54920105ca1STaeung Song { 55020105ca1STaeung Song struct perf_config_item *item; 55120105ca1STaeung Song 55220105ca1STaeung Song list_for_each_entry(item, §ion->items, node) 55320105ca1STaeung Song if (!strcmp(item->name, name)) 55420105ca1STaeung Song return item; 55520105ca1STaeung Song 55620105ca1STaeung Song return NULL; 55720105ca1STaeung Song } 55820105ca1STaeung Song 55920105ca1STaeung Song static struct perf_config_section *add_section(struct list_head *sections, 56020105ca1STaeung Song const char *section_name) 56120105ca1STaeung Song { 56220105ca1STaeung Song struct perf_config_section *section = zalloc(sizeof(*section)); 56320105ca1STaeung Song 56420105ca1STaeung Song if (!section) 56520105ca1STaeung Song return NULL; 56620105ca1STaeung Song 56720105ca1STaeung Song INIT_LIST_HEAD(§ion->items); 56820105ca1STaeung Song section->name = strdup(section_name); 56920105ca1STaeung Song if (!section->name) { 57020105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 57120105ca1STaeung Song free(section); 57220105ca1STaeung Song return NULL; 57320105ca1STaeung Song } 57420105ca1STaeung Song 57520105ca1STaeung Song list_add_tail(§ion->node, sections); 57620105ca1STaeung Song return section; 57720105ca1STaeung Song } 57820105ca1STaeung Song 57920105ca1STaeung Song static struct perf_config_item *add_config_item(struct perf_config_section *section, 58020105ca1STaeung Song const char *name) 58120105ca1STaeung Song { 58220105ca1STaeung Song struct perf_config_item *item = zalloc(sizeof(*item)); 58320105ca1STaeung Song 58420105ca1STaeung Song if (!item) 58520105ca1STaeung Song return NULL; 58620105ca1STaeung Song 58720105ca1STaeung Song item->name = strdup(name); 58820105ca1STaeung Song if (!item->name) { 58920105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 59020105ca1STaeung Song free(item); 59120105ca1STaeung Song return NULL; 59220105ca1STaeung Song } 59320105ca1STaeung Song 59420105ca1STaeung Song list_add_tail(&item->node, §ion->items); 59520105ca1STaeung Song return item; 59620105ca1STaeung Song } 59720105ca1STaeung Song 59820105ca1STaeung Song static int set_value(struct perf_config_item *item, const char *value) 59920105ca1STaeung Song { 60020105ca1STaeung Song char *val = strdup(value); 60120105ca1STaeung Song 60220105ca1STaeung Song if (!val) 60320105ca1STaeung Song return -1; 60420105ca1STaeung Song 60520105ca1STaeung Song zfree(&item->value); 60620105ca1STaeung Song item->value = val; 60720105ca1STaeung Song return 0; 60820105ca1STaeung Song } 60920105ca1STaeung Song 61020105ca1STaeung Song static int collect_config(const char *var, const char *value, 61120105ca1STaeung Song void *perf_config_set) 61220105ca1STaeung Song { 61320105ca1STaeung Song int ret = -1; 61420105ca1STaeung Song char *ptr, *key; 61520105ca1STaeung Song char *section_name, *name; 61620105ca1STaeung Song struct perf_config_section *section = NULL; 61720105ca1STaeung Song struct perf_config_item *item = NULL; 61820105ca1STaeung Song struct perf_config_set *set = perf_config_set; 6197db91f25STaeung Song struct list_head *sections; 62020105ca1STaeung Song 6217db91f25STaeung Song if (set == NULL) 6227db91f25STaeung Song return -1; 6237db91f25STaeung Song 6247db91f25STaeung Song sections = &set->sections; 62520105ca1STaeung Song key = ptr = strdup(var); 62620105ca1STaeung Song if (!key) { 62720105ca1STaeung Song pr_debug("%s: strdup failed\n", __func__); 62820105ca1STaeung Song return -1; 62920105ca1STaeung Song } 63020105ca1STaeung Song 63120105ca1STaeung Song section_name = strsep(&ptr, "."); 63220105ca1STaeung Song name = ptr; 63320105ca1STaeung Song if (name == NULL || value == NULL) 63420105ca1STaeung Song goto out_free; 63520105ca1STaeung Song 63620105ca1STaeung Song section = find_section(sections, section_name); 63720105ca1STaeung Song if (!section) { 63820105ca1STaeung Song section = add_section(sections, section_name); 63920105ca1STaeung Song if (!section) 64020105ca1STaeung Song goto out_free; 64120105ca1STaeung Song } 64220105ca1STaeung Song 64320105ca1STaeung Song item = find_config_item(name, section); 64420105ca1STaeung Song if (!item) { 64520105ca1STaeung Song item = add_config_item(section, name); 64620105ca1STaeung Song if (!item) 64720105ca1STaeung Song goto out_free; 64820105ca1STaeung Song } 64920105ca1STaeung Song 65008d090cfSTaeung Song /* perf_config_set can contain both user and system config items. 65108d090cfSTaeung Song * So we should know where each value is from. 65208d090cfSTaeung Song * The classification would be needed when a particular config file 65308d090cfSTaeung Song * is overwrited by setting feature i.e. set_config(). 65408d090cfSTaeung Song */ 65508d090cfSTaeung Song if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) { 65608d090cfSTaeung Song section->from_system_config = true; 65708d090cfSTaeung Song item->from_system_config = true; 65808d090cfSTaeung Song } else { 65908d090cfSTaeung Song section->from_system_config = false; 66008d090cfSTaeung Song item->from_system_config = false; 66108d090cfSTaeung Song } 66208d090cfSTaeung Song 66320105ca1STaeung Song ret = set_value(item, value); 66420105ca1STaeung Song 66520105ca1STaeung Song out_free: 66620105ca1STaeung Song free(key); 66754569ba4SChangbin Du return ret; 66820105ca1STaeung Song } 66920105ca1STaeung Song 67008d090cfSTaeung Song int perf_config_set__collect(struct perf_config_set *set, const char *file_name, 671c6fc018aSTaeung Song const char *var, const char *value) 672c6fc018aSTaeung Song { 67308d090cfSTaeung Song config_file_name = file_name; 674c6fc018aSTaeung Song return collect_config(var, value, set); 675c6fc018aSTaeung Song } 676c6fc018aSTaeung Song 6778beeb00fSTaeung Song static int perf_config_set__init(struct perf_config_set *set) 6788beeb00fSTaeung Song { 6798beeb00fSTaeung Song int ret = -1; 6808beeb00fSTaeung Song const char *home = NULL; 6813e00cbe8SJiri Olsa char *user_config; 6823e00cbe8SJiri Olsa struct stat st; 6838beeb00fSTaeung Song 6848beeb00fSTaeung Song /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ 6858beeb00fSTaeung Song if (config_exclusive_filename) 6868beeb00fSTaeung Song return perf_config_from_file(collect_config, config_exclusive_filename, set); 6878beeb00fSTaeung Song if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) { 6888beeb00fSTaeung Song if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0) 6898beeb00fSTaeung Song goto out; 6908beeb00fSTaeung Song } 6918beeb00fSTaeung Song 6928beeb00fSTaeung Song home = getenv("HOME"); 6938beeb00fSTaeung Song 6943e00cbe8SJiri Olsa /* 6953e00cbe8SJiri Olsa * Skip reading user config if: 6963e00cbe8SJiri Olsa * - there is no place to read it from (HOME) 6973e00cbe8SJiri Olsa * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) 6983e00cbe8SJiri Olsa */ 6993e00cbe8SJiri Olsa if (!home || !*home || !perf_config_global()) 7003e00cbe8SJiri Olsa return 0; 7013e00cbe8SJiri Olsa 7023e00cbe8SJiri Olsa user_config = strdup(mkpath("%s/.perfconfig", home)); 7038beeb00fSTaeung Song if (user_config == NULL) { 7044cf134e7SArnaldo Carvalho de Melo pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home); 7058beeb00fSTaeung Song goto out; 7068beeb00fSTaeung Song } 7078beeb00fSTaeung Song 708afc45cf5SArnaldo Carvalho de Melo if (stat(user_config, &st) < 0) { 709afc45cf5SArnaldo Carvalho de Melo if (errno == ENOENT) 710afc45cf5SArnaldo Carvalho de Melo ret = 0; 7118beeb00fSTaeung Song goto out_free; 712afc45cf5SArnaldo Carvalho de Melo } 713afc45cf5SArnaldo Carvalho de Melo 714afc45cf5SArnaldo Carvalho de Melo ret = 0; 7158beeb00fSTaeung Song 7168beeb00fSTaeung Song if (st.st_uid && (st.st_uid != geteuid())) { 7174cf134e7SArnaldo Carvalho de Melo pr_warning("File %s not owned by current user or root, ignoring it.", user_config); 7188beeb00fSTaeung Song goto out_free; 7198beeb00fSTaeung Song } 7208beeb00fSTaeung Song 721afc45cf5SArnaldo Carvalho de Melo if (st.st_size) 7228beeb00fSTaeung Song ret = perf_config_from_file(collect_config, user_config, set); 7233e00cbe8SJiri Olsa 7248beeb00fSTaeung Song out_free: 7258beeb00fSTaeung Song free(user_config); 7268beeb00fSTaeung Song out: 7278beeb00fSTaeung Song return ret; 7288beeb00fSTaeung Song } 7298beeb00fSTaeung Song 73020105ca1STaeung Song struct perf_config_set *perf_config_set__new(void) 73120105ca1STaeung Song { 73220105ca1STaeung Song struct perf_config_set *set = zalloc(sizeof(*set)); 73320105ca1STaeung Song 73420105ca1STaeung Song if (set) { 73520105ca1STaeung Song INIT_LIST_HEAD(&set->sections); 73655421b4fSTaeung Song perf_config_set__init(set); 73720105ca1STaeung Song } 73820105ca1STaeung Song 73920105ca1STaeung Song return set; 74020105ca1STaeung Song } 74120105ca1STaeung Song 742d01bd1acSArnaldo Carvalho de Melo static int perf_config__init(void) 743d01bd1acSArnaldo Carvalho de Melo { 744d01bd1acSArnaldo Carvalho de Melo if (config_set == NULL) 745d01bd1acSArnaldo Carvalho de Melo config_set = perf_config_set__new(); 746d01bd1acSArnaldo Carvalho de Melo 747d01bd1acSArnaldo Carvalho de Melo return config_set == NULL; 748d01bd1acSArnaldo Carvalho de Melo } 749d01bd1acSArnaldo Carvalho de Melo 7508a0a9c7eSTaeung Song int perf_config(config_fn_t fn, void *data) 7518a0a9c7eSTaeung Song { 7528a0a9c7eSTaeung Song int ret = 0; 7538a0a9c7eSTaeung Song char key[BUFSIZ]; 7548a0a9c7eSTaeung Song struct perf_config_section *section; 7558a0a9c7eSTaeung Song struct perf_config_item *item; 7568a0a9c7eSTaeung Song 757d01bd1acSArnaldo Carvalho de Melo if (config_set == NULL && perf_config__init()) 7588a0a9c7eSTaeung Song return -1; 7598a0a9c7eSTaeung Song 7608a0a9c7eSTaeung Song perf_config_set__for_each_entry(config_set, section, item) { 7618a0a9c7eSTaeung Song char *value = item->value; 7628a0a9c7eSTaeung Song 7638a0a9c7eSTaeung Song if (value) { 7648a0a9c7eSTaeung Song scnprintf(key, sizeof(key), "%s.%s", 7658a0a9c7eSTaeung Song section->name, item->name); 7668a0a9c7eSTaeung Song ret = fn(key, value, data); 7678a0a9c7eSTaeung Song if (ret < 0) { 7688a0a9c7eSTaeung Song pr_err("Error: wrong config key-value pair %s=%s\n", 7698a0a9c7eSTaeung Song key, value); 77022d46219SArnaldo Carvalho de Melo /* 77122d46219SArnaldo Carvalho de Melo * Can't be just a 'break', as perf_config_set__for_each_entry() 77222d46219SArnaldo Carvalho de Melo * expands to two nested for() loops. 77322d46219SArnaldo Carvalho de Melo */ 77422d46219SArnaldo Carvalho de Melo goto out; 7758a0a9c7eSTaeung Song } 7768a0a9c7eSTaeung Song } 7778a0a9c7eSTaeung Song } 77822d46219SArnaldo Carvalho de Melo out: 7798a0a9c7eSTaeung Song return ret; 7808a0a9c7eSTaeung Song } 7818a0a9c7eSTaeung Song 7828a0a9c7eSTaeung Song void perf_config__exit(void) 7838a0a9c7eSTaeung Song { 7848a0a9c7eSTaeung Song perf_config_set__delete(config_set); 7858a0a9c7eSTaeung Song config_set = NULL; 7868a0a9c7eSTaeung Song } 7878a0a9c7eSTaeung Song 7888a0a9c7eSTaeung Song void perf_config__refresh(void) 7898a0a9c7eSTaeung Song { 7908a0a9c7eSTaeung Song perf_config__exit(); 7918a0a9c7eSTaeung Song perf_config__init(); 7928a0a9c7eSTaeung Song } 7938a0a9c7eSTaeung Song 79420105ca1STaeung Song static void perf_config_item__delete(struct perf_config_item *item) 79520105ca1STaeung Song { 79620105ca1STaeung Song zfree(&item->name); 79720105ca1STaeung Song zfree(&item->value); 79820105ca1STaeung Song free(item); 79920105ca1STaeung Song } 80020105ca1STaeung Song 80120105ca1STaeung Song static void perf_config_section__purge(struct perf_config_section *section) 80220105ca1STaeung Song { 80320105ca1STaeung Song struct perf_config_item *item, *tmp; 80420105ca1STaeung Song 80520105ca1STaeung Song list_for_each_entry_safe(item, tmp, §ion->items, node) { 80620105ca1STaeung Song list_del_init(&item->node); 80720105ca1STaeung Song perf_config_item__delete(item); 80820105ca1STaeung Song } 80920105ca1STaeung Song } 81020105ca1STaeung Song 81120105ca1STaeung Song static void perf_config_section__delete(struct perf_config_section *section) 81220105ca1STaeung Song { 81320105ca1STaeung Song perf_config_section__purge(section); 81420105ca1STaeung Song zfree(§ion->name); 81520105ca1STaeung Song free(section); 81620105ca1STaeung Song } 81720105ca1STaeung Song 81820105ca1STaeung Song static void perf_config_set__purge(struct perf_config_set *set) 81920105ca1STaeung Song { 82020105ca1STaeung Song struct perf_config_section *section, *tmp; 82120105ca1STaeung Song 82220105ca1STaeung Song list_for_each_entry_safe(section, tmp, &set->sections, node) { 82320105ca1STaeung Song list_del_init(§ion->node); 82420105ca1STaeung Song perf_config_section__delete(section); 82520105ca1STaeung Song } 82620105ca1STaeung Song } 82720105ca1STaeung Song 82820105ca1STaeung Song void perf_config_set__delete(struct perf_config_set *set) 82920105ca1STaeung Song { 830826424ccSTaeung Song if (set == NULL) 831826424ccSTaeung Song return; 832826424ccSTaeung Song 83320105ca1STaeung Song perf_config_set__purge(set); 83420105ca1STaeung Song free(set); 83520105ca1STaeung Song } 83620105ca1STaeung Song 83786470930SIngo Molnar /* 83886470930SIngo Molnar * Call this to report error for your variable that should not 83986470930SIngo Molnar * get a boolean value (i.e. "[my] var" means "true"). 84086470930SIngo Molnar */ 84186470930SIngo Molnar int config_error_nonbool(const char *var) 84286470930SIngo Molnar { 84362d94b00SArnaldo Carvalho de Melo pr_err("Missing value for '%s'", var); 84462d94b00SArnaldo Carvalho de Melo return -1; 84586470930SIngo Molnar } 84645de34bbSStephane Eranian 84799ce8e9fSJiri Olsa void set_buildid_dir(const char *dir) 84845de34bbSStephane Eranian { 84999ce8e9fSJiri Olsa if (dir) 85075c375c0SSihyeon Jang scnprintf(buildid_dir, MAXPATHLEN, "%s", dir); 85145de34bbSStephane Eranian 85245de34bbSStephane Eranian /* default to $HOME/.debug */ 85345de34bbSStephane Eranian if (buildid_dir[0] == '\0') { 85437194f44STaeung Song char *home = getenv("HOME"); 85537194f44STaeung Song 85637194f44STaeung Song if (home) { 85775c375c0SSihyeon Jang snprintf(buildid_dir, MAXPATHLEN, "%s/%s", 85837194f44STaeung Song home, DEBUG_CACHE_DIR); 85945de34bbSStephane Eranian } else { 86045de34bbSStephane Eranian strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1); 86145de34bbSStephane Eranian } 86245de34bbSStephane Eranian buildid_dir[MAXPATHLEN-1] = '\0'; 86345de34bbSStephane Eranian } 86445de34bbSStephane Eranian /* for communicating with external commands */ 86545de34bbSStephane Eranian setenv("PERF_BUILDID_DIR", buildid_dir, 1); 86645de34bbSStephane Eranian } 867