xref: /openbmc/linux/tools/perf/util/config.c (revision 701677b95764c06bb058c92be11c3a4ad25ab5f2)
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 */
20d778a778SPaul A. Clarke #include "util/stat.h"  /* perf_stat__set_big_num */
21112cb561SSong Liu #include "util/evsel.h"  /* evsel__hw_names, evsel__use_bpf_counters */
22*701677b9SIan Rogers #include "util/srcline.h"  /* addr2line_timeout_ms */
234cb3c6d5SArnaldo Carvalho de Melo #include "build-id.h"
244cb3c6d5SArnaldo Carvalho de Melo #include "debug.h"
2520105ca1STaeung Song #include "config.h"
267a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h>
277a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
28f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
297a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h>
308e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h>
317f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
323052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h>
333d689ed6SArnaldo Carvalho de Melo 
3486470930SIngo Molnar #define MAXNAME (256)
3586470930SIngo Molnar 
3645de34bbSStephane Eranian #define DEBUG_CACHE_DIR ".debug"
3745de34bbSStephane Eranian 
3845de34bbSStephane Eranian 
3945de34bbSStephane Eranian char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
4045de34bbSStephane Eranian 
4186470930SIngo Molnar static FILE *config_file;
4286470930SIngo Molnar static const char *config_file_name;
4386470930SIngo Molnar static int config_linenr;
4486470930SIngo Molnar static int config_file_eof;
458a0a9c7eSTaeung Song static struct perf_config_set *config_set;
4686470930SIngo Molnar 
47c7ac2417STaeung Song const char *config_exclusive_filename;
4886470930SIngo Molnar 
4986470930SIngo Molnar static int get_next_char(void)
5086470930SIngo Molnar {
5186470930SIngo Molnar 	int c;
5286470930SIngo Molnar 	FILE *f;
5386470930SIngo Molnar 
5486470930SIngo Molnar 	c = '\n';
5586470930SIngo Molnar 	if ((f = config_file) != NULL) {
5686470930SIngo Molnar 		c = fgetc(f);
5786470930SIngo Molnar 		if (c == '\r') {
5886470930SIngo Molnar 			/* DOS like systems */
5986470930SIngo Molnar 			c = fgetc(f);
6086470930SIngo Molnar 			if (c != '\n') {
6186470930SIngo Molnar 				ungetc(c, f);
6286470930SIngo Molnar 				c = '\r';
6386470930SIngo Molnar 			}
6486470930SIngo Molnar 		}
6586470930SIngo Molnar 		if (c == '\n')
6686470930SIngo Molnar 			config_linenr++;
6786470930SIngo Molnar 		if (c == EOF) {
6886470930SIngo Molnar 			config_file_eof = 1;
6986470930SIngo Molnar 			c = '\n';
7086470930SIngo Molnar 		}
7186470930SIngo Molnar 	}
7286470930SIngo Molnar 	return c;
7386470930SIngo Molnar }
7486470930SIngo Molnar 
7586470930SIngo Molnar static char *parse_value(void)
7686470930SIngo Molnar {
7786470930SIngo Molnar 	static char value[1024];
78f37a291cSIngo Molnar 	int quote = 0, comment = 0, space = 0;
79f37a291cSIngo Molnar 	size_t len = 0;
8086470930SIngo Molnar 
8186470930SIngo Molnar 	for (;;) {
8286470930SIngo Molnar 		int c = get_next_char();
83f37a291cSIngo Molnar 
8486470930SIngo Molnar 		if (len >= sizeof(value) - 1)
8586470930SIngo Molnar 			return NULL;
8686470930SIngo Molnar 		if (c == '\n') {
8786470930SIngo Molnar 			if (quote)
8886470930SIngo Molnar 				return NULL;
8986470930SIngo Molnar 			value[len] = 0;
9086470930SIngo Molnar 			return value;
9186470930SIngo Molnar 		}
9286470930SIngo Molnar 		if (comment)
9386470930SIngo Molnar 			continue;
9486470930SIngo Molnar 		if (isspace(c) && !quote) {
9586470930SIngo Molnar 			space = 1;
9686470930SIngo Molnar 			continue;
9786470930SIngo Molnar 		}
9886470930SIngo Molnar 		if (!quote) {
9986470930SIngo Molnar 			if (c == ';' || c == '#') {
10086470930SIngo Molnar 				comment = 1;
10186470930SIngo Molnar 				continue;
10286470930SIngo Molnar 			}
10386470930SIngo Molnar 		}
10486470930SIngo Molnar 		if (space) {
10586470930SIngo Molnar 			if (len)
10686470930SIngo Molnar 				value[len++] = ' ';
10786470930SIngo Molnar 			space = 0;
10886470930SIngo Molnar 		}
10986470930SIngo Molnar 		if (c == '\\') {
11086470930SIngo Molnar 			c = get_next_char();
11186470930SIngo Molnar 			switch (c) {
11286470930SIngo Molnar 			case '\n':
11386470930SIngo Molnar 				continue;
11486470930SIngo Molnar 			case 't':
11586470930SIngo Molnar 				c = '\t';
11686470930SIngo Molnar 				break;
11786470930SIngo Molnar 			case 'b':
11886470930SIngo Molnar 				c = '\b';
11986470930SIngo Molnar 				break;
12086470930SIngo Molnar 			case 'n':
12186470930SIngo Molnar 				c = '\n';
12286470930SIngo Molnar 				break;
12386470930SIngo Molnar 			/* Some characters escape as themselves */
12486470930SIngo Molnar 			case '\\': case '"':
12586470930SIngo Molnar 				break;
12686470930SIngo Molnar 			/* Reject unknown escape sequences */
12786470930SIngo Molnar 			default:
12886470930SIngo Molnar 				return NULL;
12986470930SIngo Molnar 			}
13086470930SIngo Molnar 			value[len++] = c;
13186470930SIngo Molnar 			continue;
13286470930SIngo Molnar 		}
13386470930SIngo Molnar 		if (c == '"') {
13486470930SIngo Molnar 			quote = 1-quote;
13586470930SIngo Molnar 			continue;
13686470930SIngo Molnar 		}
13786470930SIngo Molnar 		value[len++] = c;
13886470930SIngo Molnar 	}
13986470930SIngo Molnar }
14086470930SIngo Molnar 
14186470930SIngo Molnar static inline int iskeychar(int c)
14286470930SIngo Molnar {
1438dc7c651SArnaldo Carvalho de Melo 	return isalnum(c) || c == '-' || c == '_';
14486470930SIngo Molnar }
14586470930SIngo Molnar 
14686470930SIngo Molnar static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
14786470930SIngo Molnar {
14886470930SIngo Molnar 	int c;
14986470930SIngo Molnar 	char *value;
15086470930SIngo Molnar 
15186470930SIngo Molnar 	/* Get the full name */
15286470930SIngo Molnar 	for (;;) {
15386470930SIngo Molnar 		c = get_next_char();
15486470930SIngo Molnar 		if (config_file_eof)
15586470930SIngo Molnar 			break;
15686470930SIngo Molnar 		if (!iskeychar(c))
15786470930SIngo Molnar 			break;
15845de34bbSStephane Eranian 		name[len++] = c;
15986470930SIngo Molnar 		if (len >= MAXNAME)
16086470930SIngo Molnar 			return -1;
16186470930SIngo Molnar 	}
16286470930SIngo Molnar 	name[len] = 0;
16386470930SIngo Molnar 	while (c == ' ' || c == '\t')
16486470930SIngo Molnar 		c = get_next_char();
16586470930SIngo Molnar 
16686470930SIngo Molnar 	value = NULL;
16786470930SIngo Molnar 	if (c != '\n') {
16886470930SIngo Molnar 		if (c != '=')
16986470930SIngo Molnar 			return -1;
17086470930SIngo Molnar 		value = parse_value();
17186470930SIngo Molnar 		if (!value)
17286470930SIngo Molnar 			return -1;
17386470930SIngo Molnar 	}
17486470930SIngo Molnar 	return fn(name, value, data);
17586470930SIngo Molnar }
17686470930SIngo Molnar 
17786470930SIngo Molnar static int get_extended_base_var(char *name, int baselen, int c)
17886470930SIngo Molnar {
17986470930SIngo Molnar 	do {
18086470930SIngo Molnar 		if (c == '\n')
18186470930SIngo Molnar 			return -1;
18286470930SIngo Molnar 		c = get_next_char();
18386470930SIngo Molnar 	} while (isspace(c));
18486470930SIngo Molnar 
18586470930SIngo Molnar 	/* We require the format to be '[base "extension"]' */
18686470930SIngo Molnar 	if (c != '"')
18786470930SIngo Molnar 		return -1;
18886470930SIngo Molnar 	name[baselen++] = '.';
18986470930SIngo Molnar 
19086470930SIngo Molnar 	for (;;) {
19183a0944fSIngo Molnar 		int ch = get_next_char();
19283a0944fSIngo Molnar 
19383a0944fSIngo Molnar 		if (ch == '\n')
19486470930SIngo Molnar 			return -1;
19583a0944fSIngo Molnar 		if (ch == '"')
19686470930SIngo Molnar 			break;
19783a0944fSIngo Molnar 		if (ch == '\\') {
19883a0944fSIngo Molnar 			ch = get_next_char();
19983a0944fSIngo Molnar 			if (ch == '\n')
20086470930SIngo Molnar 				return -1;
20186470930SIngo Molnar 		}
20283a0944fSIngo Molnar 		name[baselen++] = ch;
20386470930SIngo Molnar 		if (baselen > MAXNAME / 2)
20486470930SIngo Molnar 			return -1;
20586470930SIngo Molnar 	}
20686470930SIngo Molnar 
20786470930SIngo Molnar 	/* Final ']' */
20886470930SIngo Molnar 	if (get_next_char() != ']')
20986470930SIngo Molnar 		return -1;
21086470930SIngo Molnar 	return baselen;
21186470930SIngo Molnar }
21286470930SIngo Molnar 
21386470930SIngo Molnar static int get_base_var(char *name)
21486470930SIngo Molnar {
21586470930SIngo Molnar 	int baselen = 0;
21686470930SIngo Molnar 
21786470930SIngo Molnar 	for (;;) {
21886470930SIngo Molnar 		int c = get_next_char();
21986470930SIngo Molnar 		if (config_file_eof)
22086470930SIngo Molnar 			return -1;
22186470930SIngo Molnar 		if (c == ']')
22286470930SIngo Molnar 			return baselen;
22386470930SIngo Molnar 		if (isspace(c))
22486470930SIngo Molnar 			return get_extended_base_var(name, baselen, c);
22586470930SIngo Molnar 		if (!iskeychar(c) && c != '.')
22686470930SIngo Molnar 			return -1;
22786470930SIngo Molnar 		if (baselen > MAXNAME / 2)
22886470930SIngo Molnar 			return -1;
22986470930SIngo Molnar 		name[baselen++] = tolower(c);
23086470930SIngo Molnar 	}
23186470930SIngo Molnar }
23286470930SIngo Molnar 
23386470930SIngo Molnar static int perf_parse_file(config_fn_t fn, void *data)
23486470930SIngo Molnar {
23586470930SIngo Molnar 	int comment = 0;
23686470930SIngo Molnar 	int baselen = 0;
23786470930SIngo Molnar 	static char var[MAXNAME];
23886470930SIngo Molnar 
23986470930SIngo Molnar 	/* U+FEFF Byte Order Mark in UTF8 */
24086470930SIngo Molnar 	static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
24186470930SIngo Molnar 	const unsigned char *bomptr = utf8_bom;
24286470930SIngo Molnar 
24386470930SIngo Molnar 	for (;;) {
24449757c9cSJiri Olsa 		int line, c = get_next_char();
24549757c9cSJiri Olsa 
24686470930SIngo Molnar 		if (bomptr && *bomptr) {
24786470930SIngo Molnar 			/* We are at the file beginning; skip UTF8-encoded BOM
24886470930SIngo Molnar 			 * if present. Sane editors won't put this in on their
24986470930SIngo Molnar 			 * own, but e.g. Windows Notepad will do it happily. */
25086470930SIngo Molnar 			if ((unsigned char) c == *bomptr) {
25186470930SIngo Molnar 				bomptr++;
25286470930SIngo Molnar 				continue;
25386470930SIngo Molnar 			} else {
25486470930SIngo Molnar 				/* Do not tolerate partial BOM. */
25586470930SIngo Molnar 				if (bomptr != utf8_bom)
25686470930SIngo Molnar 					break;
25786470930SIngo Molnar 				/* No BOM at file beginning. Cool. */
25886470930SIngo Molnar 				bomptr = NULL;
25986470930SIngo Molnar 			}
26086470930SIngo Molnar 		}
26186470930SIngo Molnar 		if (c == '\n') {
26286470930SIngo Molnar 			if (config_file_eof)
26386470930SIngo Molnar 				return 0;
26486470930SIngo Molnar 			comment = 0;
26586470930SIngo Molnar 			continue;
26686470930SIngo Molnar 		}
26786470930SIngo Molnar 		if (comment || isspace(c))
26886470930SIngo Molnar 			continue;
26986470930SIngo Molnar 		if (c == '#' || c == ';') {
27086470930SIngo Molnar 			comment = 1;
27186470930SIngo Molnar 			continue;
27286470930SIngo Molnar 		}
27386470930SIngo Molnar 		if (c == '[') {
27486470930SIngo Molnar 			baselen = get_base_var(var);
27586470930SIngo Molnar 			if (baselen <= 0)
27686470930SIngo Molnar 				break;
27786470930SIngo Molnar 			var[baselen++] = '.';
27886470930SIngo Molnar 			var[baselen] = 0;
27986470930SIngo Molnar 			continue;
28086470930SIngo Molnar 		}
28186470930SIngo Molnar 		if (!isalpha(c))
28286470930SIngo Molnar 			break;
28386470930SIngo Molnar 		var[baselen] = tolower(c);
28449757c9cSJiri Olsa 
28549757c9cSJiri Olsa 		/*
28649757c9cSJiri Olsa 		 * The get_value function might or might not reach the '\n',
28749757c9cSJiri Olsa 		 * so saving the current line number for error reporting.
28849757c9cSJiri Olsa 		 */
28949757c9cSJiri Olsa 		line = config_linenr;
29049757c9cSJiri Olsa 		if (get_value(fn, data, var, baselen+1) < 0) {
29149757c9cSJiri Olsa 			config_linenr = line;
29286470930SIngo Molnar 			break;
29386470930SIngo Molnar 		}
29449757c9cSJiri Olsa 	}
29578f71c99STaeung Song 	pr_err("bad config file line %d in %s\n", config_linenr, config_file_name);
29678f71c99STaeung Song 	return -1;
29786470930SIngo Molnar }
29886470930SIngo Molnar 
29986470930SIngo Molnar static int parse_unit_factor(const char *end, unsigned long *val)
30086470930SIngo Molnar {
30186470930SIngo Molnar 	if (!*end)
30286470930SIngo Molnar 		return 1;
30386470930SIngo Molnar 	else if (!strcasecmp(end, "k")) {
30486470930SIngo Molnar 		*val *= 1024;
30586470930SIngo Molnar 		return 1;
30686470930SIngo Molnar 	}
30786470930SIngo Molnar 	else if (!strcasecmp(end, "m")) {
30886470930SIngo Molnar 		*val *= 1024 * 1024;
30986470930SIngo Molnar 		return 1;
31086470930SIngo Molnar 	}
31186470930SIngo Molnar 	else if (!strcasecmp(end, "g")) {
31286470930SIngo Molnar 		*val *= 1024 * 1024 * 1024;
31386470930SIngo Molnar 		return 1;
31486470930SIngo Molnar 	}
31586470930SIngo Molnar 	return 0;
31686470930SIngo Molnar }
31786470930SIngo Molnar 
31894c0655fSJiri Olsa static int perf_parse_llong(const char *value, long long *ret)
31994c0655fSJiri Olsa {
32094c0655fSJiri Olsa 	if (value && *value) {
32194c0655fSJiri Olsa 		char *end;
32294c0655fSJiri Olsa 		long long val = strtoll(value, &end, 0);
32394c0655fSJiri Olsa 		unsigned long factor = 1;
32494c0655fSJiri Olsa 
32594c0655fSJiri Olsa 		if (!parse_unit_factor(end, &factor))
32694c0655fSJiri Olsa 			return 0;
32794c0655fSJiri Olsa 		*ret = val * factor;
32894c0655fSJiri Olsa 		return 1;
32994c0655fSJiri Olsa 	}
33094c0655fSJiri Olsa 	return 0;
33194c0655fSJiri Olsa }
33294c0655fSJiri Olsa 
33386470930SIngo Molnar static int perf_parse_long(const char *value, long *ret)
33486470930SIngo Molnar {
33586470930SIngo Molnar 	if (value && *value) {
33686470930SIngo Molnar 		char *end;
33786470930SIngo Molnar 		long val = strtol(value, &end, 0);
33886470930SIngo Molnar 		unsigned long factor = 1;
33986470930SIngo Molnar 		if (!parse_unit_factor(end, &factor))
34086470930SIngo Molnar 			return 0;
34186470930SIngo Molnar 		*ret = val * factor;
34286470930SIngo Molnar 		return 1;
34386470930SIngo Molnar 	}
34486470930SIngo Molnar 	return 0;
34586470930SIngo Molnar }
34686470930SIngo Molnar 
34725ce4bb8SArnaldo Carvalho de Melo static void bad_config(const char *name)
34886470930SIngo Molnar {
34986470930SIngo Molnar 	if (config_file_name)
35025ce4bb8SArnaldo Carvalho de Melo 		pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name);
35125ce4bb8SArnaldo Carvalho de Melo 	else
35225ce4bb8SArnaldo Carvalho de Melo 		pr_warning("bad config value for '%s', ignoring...\n", name);
35386470930SIngo Molnar }
35486470930SIngo Molnar 
35525ce4bb8SArnaldo Carvalho de Melo int perf_config_u64(u64 *dest, const char *name, const char *value)
35694c0655fSJiri Olsa {
35794c0655fSJiri Olsa 	long long ret = 0;
35894c0655fSJiri Olsa 
35925ce4bb8SArnaldo Carvalho de Melo 	if (!perf_parse_llong(value, &ret)) {
36025ce4bb8SArnaldo Carvalho de Melo 		bad_config(name);
36125ce4bb8SArnaldo Carvalho de Melo 		return -1;
36294c0655fSJiri Olsa 	}
36394c0655fSJiri Olsa 
36425ce4bb8SArnaldo Carvalho de Melo 	*dest = ret;
36525ce4bb8SArnaldo Carvalho de Melo 	return 0;
36625ce4bb8SArnaldo Carvalho de Melo }
36725ce4bb8SArnaldo Carvalho de Melo 
36825ce4bb8SArnaldo Carvalho de Melo int perf_config_int(int *dest, const char *name, const char *value)
36986470930SIngo Molnar {
37086470930SIngo Molnar 	long ret = 0;
37125ce4bb8SArnaldo Carvalho de Melo 	if (!perf_parse_long(value, &ret)) {
37225ce4bb8SArnaldo Carvalho de Melo 		bad_config(name);
37325ce4bb8SArnaldo Carvalho de Melo 		return -1;
37425ce4bb8SArnaldo Carvalho de Melo 	}
37525ce4bb8SArnaldo Carvalho de Melo 	*dest = ret;
37625ce4bb8SArnaldo Carvalho de Melo 	return 0;
37786470930SIngo Molnar }
37886470930SIngo Molnar 
3797b43b697SRavi Bangoria int perf_config_u8(u8 *dest, const char *name, const char *value)
3807b43b697SRavi Bangoria {
3817b43b697SRavi Bangoria 	long ret = 0;
3827b43b697SRavi Bangoria 
3837b43b697SRavi Bangoria 	if (!perf_parse_long(value, &ret)) {
3847b43b697SRavi Bangoria 		bad_config(name);
3857b43b697SRavi Bangoria 		return -1;
3867b43b697SRavi Bangoria 	}
3877b43b697SRavi Bangoria 	*dest = ret;
3887b43b697SRavi Bangoria 	return 0;
3897b43b697SRavi Bangoria }
3907b43b697SRavi Bangoria 
391a41794cdSArnaldo Carvalho de Melo static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
39286470930SIngo Molnar {
39325ce4bb8SArnaldo Carvalho de Melo 	int ret;
39425ce4bb8SArnaldo Carvalho de Melo 
39586470930SIngo Molnar 	*is_bool = 1;
39686470930SIngo Molnar 	if (!value)
39786470930SIngo Molnar 		return 1;
39886470930SIngo Molnar 	if (!*value)
39986470930SIngo Molnar 		return 0;
40086470930SIngo Molnar 	if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
40186470930SIngo Molnar 		return 1;
40286470930SIngo Molnar 	if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
40386470930SIngo Molnar 		return 0;
40486470930SIngo Molnar 	*is_bool = 0;
40525ce4bb8SArnaldo Carvalho de Melo 	return perf_config_int(&ret, name, value) < 0 ? -1 : ret;
40686470930SIngo Molnar }
40786470930SIngo Molnar 
40886470930SIngo Molnar int perf_config_bool(const char *name, const char *value)
40986470930SIngo Molnar {
41086470930SIngo Molnar 	int discard;
41186470930SIngo Molnar 	return !!perf_config_bool_or_int(name, value, &discard);
41286470930SIngo Molnar }
41386470930SIngo Molnar 
414814b3f51SArnaldo Carvalho de Melo static const char *perf_config_dirname(const char *name, const char *value)
41545de34bbSStephane Eranian {
41645de34bbSStephane Eranian 	if (!name)
41745de34bbSStephane Eranian 		return NULL;
41845de34bbSStephane Eranian 	return value;
41945de34bbSStephane Eranian }
42045de34bbSStephane Eranian 
4219cb5987cSTaeung Song static int perf_buildid_config(const char *var, const char *value)
4229cb5987cSTaeung Song {
4239cb5987cSTaeung Song 	/* same dir for all commands */
4249cb5987cSTaeung Song 	if (!strcmp(var, "buildid.dir")) {
425d8e28654SVinson Lee 		const char *dir = perf_config_dirname(var, value);
4269cb5987cSTaeung Song 
427ecc4c561SArnaldo Carvalho de Melo 		if (!dir) {
428ecc4c561SArnaldo Carvalho de Melo 			pr_err("Invalid buildid directory!\n");
4299cb5987cSTaeung Song 			return -1;
430ecc4c561SArnaldo Carvalho de Melo 		}
431d8e28654SVinson Lee 		strncpy(buildid_dir, dir, MAXPATHLEN-1);
4329cb5987cSTaeung Song 		buildid_dir[MAXPATHLEN-1] = '\0';
4339cb5987cSTaeung Song 	}
4349cb5987cSTaeung Song 
4359cb5987cSTaeung Song 	return 0;
4369cb5987cSTaeung Song }
4379cb5987cSTaeung Song 
438*701677b9SIan Rogers static int perf_default_core_config(const char *var, const char *value)
43986470930SIngo Molnar {
4403fcb10e4SMark Drayton 	if (!strcmp(var, "core.proc-map-timeout"))
4413fcb10e4SMark Drayton 		proc_map_timeout = strtoul(value, NULL, 10);
4423fcb10e4SMark Drayton 
443*701677b9SIan Rogers 	if (!strcmp(var, "core.addr2line-timeout"))
444*701677b9SIan Rogers 		addr2line_timeout_ms = strtoul(value, NULL, 10);
445*701677b9SIan Rogers 
446395cf969SPaul Bolle 	/* Add other config variables here. */
44786470930SIngo Molnar 	return 0;
44886470930SIngo Molnar }
44986470930SIngo Molnar 
450c8302367SJiri Olsa static int perf_ui_config(const char *var, const char *value)
451c8302367SJiri Olsa {
452c8302367SJiri Olsa 	/* Add other config variables here. */
453ecc4c561SArnaldo Carvalho de Melo 	if (!strcmp(var, "ui.show-headers"))
454c8302367SJiri Olsa 		symbol_conf.show_hist_headers = perf_config_bool(var, value);
455ecc4c561SArnaldo Carvalho de Melo 
456c8302367SJiri Olsa 	return 0;
457c8302367SJiri Olsa }
458c8302367SJiri Olsa 
459d778a778SPaul A. Clarke static int perf_stat_config(const char *var, const char *value)
460d778a778SPaul A. Clarke {
461d778a778SPaul A. Clarke 	if (!strcmp(var, "stat.big-num"))
462d778a778SPaul A. Clarke 		perf_stat__set_big_num(perf_config_bool(var, value));
463d778a778SPaul A. Clarke 
4640bdad978SJin Yao 	if (!strcmp(var, "stat.no-csv-summary"))
4650bdad978SJin Yao 		perf_stat__set_no_csv_summary(perf_config_bool(var, value));
4660bdad978SJin Yao 
467112cb561SSong Liu 	if (!strcmp(var, "stat.bpf-counter-events"))
468112cb561SSong Liu 		evsel__bpf_counter_events = strdup(value);
469112cb561SSong Liu 
470d778a778SPaul A. Clarke 	/* Add other config variables here. */
471d778a778SPaul A. Clarke 	return 0;
472d778a778SPaul A. Clarke }
473d778a778SPaul A. Clarke 
4741d037ca1SIrina Tirdea int perf_default_config(const char *var, const char *value,
4751d037ca1SIrina Tirdea 			void *dummy __maybe_unused)
47686470930SIngo Molnar {
4778e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "core."))
47886470930SIngo Molnar 		return perf_default_core_config(var, value);
47986470930SIngo Molnar 
4808e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "hist."))
4810b93da17SNamhyung Kim 		return perf_hist_config(var, value);
4820b93da17SNamhyung Kim 
4838e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "ui."))
484c8302367SJiri Olsa 		return perf_ui_config(var, value);
485c8302367SJiri Olsa 
4868e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "call-graph."))
4872b9240caSNamhyung Kim 		return perf_callchain_config(var, value);
4882b9240caSNamhyung Kim 
4898e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "llvm."))
490aa61fd05SWang Nan 		return perf_llvm_config(var, value);
491aa61fd05SWang Nan 
4928e99b6d4SArnaldo Carvalho de Melo 	if (strstarts(var, "buildid."))
4939cb5987cSTaeung Song 		return perf_buildid_config(var, value);
4949cb5987cSTaeung Song 
495d778a778SPaul A. Clarke 	if (strstarts(var, "stat."))
496d778a778SPaul A. Clarke 		return perf_stat_config(var, value);
497d778a778SPaul A. Clarke 
498395cf969SPaul Bolle 	/* Add other config variables here. */
49986470930SIngo Molnar 	return 0;
50086470930SIngo Molnar }
50186470930SIngo Molnar 
50264b9705bSJiri Olsa static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
50386470930SIngo Molnar {
50486470930SIngo Molnar 	int ret;
50586470930SIngo Molnar 	FILE *f = fopen(filename, "r");
50686470930SIngo Molnar 
50786470930SIngo Molnar 	ret = -1;
50886470930SIngo Molnar 	if (f) {
50986470930SIngo Molnar 		config_file = f;
51086470930SIngo Molnar 		config_file_name = filename;
51186470930SIngo Molnar 		config_linenr = 1;
51286470930SIngo Molnar 		config_file_eof = 0;
51386470930SIngo Molnar 		ret = perf_parse_file(fn, data);
51486470930SIngo Molnar 		fclose(f);
51586470930SIngo Molnar 		config_file_name = NULL;
51686470930SIngo Molnar 	}
51786470930SIngo Molnar 	return ret;
51886470930SIngo Molnar }
51986470930SIngo Molnar 
520c7ac2417STaeung Song const char *perf_etc_perfconfig(void)
52186470930SIngo Molnar {
52286470930SIngo Molnar 	static const char *system_wide;
52386470930SIngo Molnar 	if (!system_wide)
52486470930SIngo Molnar 		system_wide = system_path(ETC_PERFCONFIG);
52586470930SIngo Molnar 	return system_wide;
52686470930SIngo Molnar }
52786470930SIngo Molnar 
52886470930SIngo Molnar static int perf_env_bool(const char *k, int def)
52986470930SIngo Molnar {
53086470930SIngo Molnar 	const char *v = getenv(k);
53186470930SIngo Molnar 	return v ? perf_config_bool(k, v) : def;
53286470930SIngo Molnar }
53386470930SIngo Molnar 
534b2946282SJiri Olsa int perf_config_system(void)
53586470930SIngo Molnar {
53686470930SIngo Molnar 	return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
53786470930SIngo Molnar }
53886470930SIngo Molnar 
539e8b2db07SJiri Olsa int perf_config_global(void)
54086470930SIngo Molnar {
54186470930SIngo Molnar 	return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
54286470930SIngo Molnar }
54386470930SIngo Molnar 
544f5f03e19SJiri Olsa static char *home_perfconfig(void)
545f5f03e19SJiri Olsa {
546f5f03e19SJiri Olsa 	const char *home = NULL;
547f5f03e19SJiri Olsa 	char *config;
548f5f03e19SJiri Olsa 	struct stat st;
549370ce164SIan Rogers 	char path[PATH_MAX];
550f5f03e19SJiri Olsa 
551f5f03e19SJiri Olsa 	home = getenv("HOME");
552f5f03e19SJiri Olsa 
553f5f03e19SJiri Olsa 	/*
554f5f03e19SJiri Olsa 	 * Skip reading user config if:
555f5f03e19SJiri Olsa 	 *   - there is no place to read it from (HOME)
556f5f03e19SJiri Olsa 	 *   - we are asked not to (PERF_CONFIG_NOGLOBAL=1)
557f5f03e19SJiri Olsa 	 */
558f5f03e19SJiri Olsa 	if (!home || !*home || !perf_config_global())
559f5f03e19SJiri Olsa 		return NULL;
560f5f03e19SJiri Olsa 
561370ce164SIan Rogers 	config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home));
562f5f03e19SJiri Olsa 	if (config == NULL) {
5630cef66a9SYang Jihong 		pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home);
564f5f03e19SJiri Olsa 		return NULL;
565f5f03e19SJiri Olsa 	}
566f5f03e19SJiri Olsa 
567f5f03e19SJiri Olsa 	if (stat(config, &st) < 0)
568f5f03e19SJiri Olsa 		goto out_free;
569f5f03e19SJiri Olsa 
570f5f03e19SJiri Olsa 	if (st.st_uid && (st.st_uid != geteuid())) {
5710cef66a9SYang Jihong 		pr_warning("File %s not owned by current user or root, ignoring it.\n", config);
572f5f03e19SJiri Olsa 		goto out_free;
573f5f03e19SJiri Olsa 	}
574f5f03e19SJiri Olsa 
575f5f03e19SJiri Olsa 	if (st.st_size)
576f5f03e19SJiri Olsa 		return config;
577f5f03e19SJiri Olsa 
578f5f03e19SJiri Olsa out_free:
579f5f03e19SJiri Olsa 	free(config);
580f5f03e19SJiri Olsa 	return NULL;
581f5f03e19SJiri Olsa }
582f5f03e19SJiri Olsa 
583f5f03e19SJiri Olsa const char *perf_home_perfconfig(void)
584f5f03e19SJiri Olsa {
585f5f03e19SJiri Olsa 	static const char *config;
586f5f03e19SJiri Olsa 	static bool failed;
587f5f03e19SJiri Olsa 
588261f4911SArnaldo Carvalho de Melo 	if (failed || config)
589261f4911SArnaldo Carvalho de Melo 		return config;
590261f4911SArnaldo Carvalho de Melo 
591261f4911SArnaldo Carvalho de Melo 	config = home_perfconfig();
592f5f03e19SJiri Olsa 	if (!config)
593f5f03e19SJiri Olsa 		failed = true;
594f5f03e19SJiri Olsa 
595f5f03e19SJiri Olsa 	return config;
596f5f03e19SJiri Olsa }
597f5f03e19SJiri Olsa 
59820105ca1STaeung Song static struct perf_config_section *find_section(struct list_head *sections,
59920105ca1STaeung Song 						const char *section_name)
60020105ca1STaeung Song {
60120105ca1STaeung Song 	struct perf_config_section *section;
60220105ca1STaeung Song 
60320105ca1STaeung Song 	list_for_each_entry(section, sections, node)
60420105ca1STaeung Song 		if (!strcmp(section->name, section_name))
60520105ca1STaeung Song 			return section;
60620105ca1STaeung Song 
60720105ca1STaeung Song 	return NULL;
60820105ca1STaeung Song }
60920105ca1STaeung Song 
61020105ca1STaeung Song static struct perf_config_item *find_config_item(const char *name,
61120105ca1STaeung Song 						 struct perf_config_section *section)
61220105ca1STaeung Song {
61320105ca1STaeung Song 	struct perf_config_item *item;
61420105ca1STaeung Song 
61520105ca1STaeung Song 	list_for_each_entry(item, &section->items, node)
61620105ca1STaeung Song 		if (!strcmp(item->name, name))
61720105ca1STaeung Song 			return item;
61820105ca1STaeung Song 
61920105ca1STaeung Song 	return NULL;
62020105ca1STaeung Song }
62120105ca1STaeung Song 
62220105ca1STaeung Song static struct perf_config_section *add_section(struct list_head *sections,
62320105ca1STaeung Song 					       const char *section_name)
62420105ca1STaeung Song {
62520105ca1STaeung Song 	struct perf_config_section *section = zalloc(sizeof(*section));
62620105ca1STaeung Song 
62720105ca1STaeung Song 	if (!section)
62820105ca1STaeung Song 		return NULL;
62920105ca1STaeung Song 
63020105ca1STaeung Song 	INIT_LIST_HEAD(&section->items);
63120105ca1STaeung Song 	section->name = strdup(section_name);
63220105ca1STaeung Song 	if (!section->name) {
63320105ca1STaeung Song 		pr_debug("%s: strdup failed\n", __func__);
63420105ca1STaeung Song 		free(section);
63520105ca1STaeung Song 		return NULL;
63620105ca1STaeung Song 	}
63720105ca1STaeung Song 
63820105ca1STaeung Song 	list_add_tail(&section->node, sections);
63920105ca1STaeung Song 	return section;
64020105ca1STaeung Song }
64120105ca1STaeung Song 
64220105ca1STaeung Song static struct perf_config_item *add_config_item(struct perf_config_section *section,
64320105ca1STaeung Song 						const char *name)
64420105ca1STaeung Song {
64520105ca1STaeung Song 	struct perf_config_item *item = zalloc(sizeof(*item));
64620105ca1STaeung Song 
64720105ca1STaeung Song 	if (!item)
64820105ca1STaeung Song 		return NULL;
64920105ca1STaeung Song 
65020105ca1STaeung Song 	item->name = strdup(name);
65120105ca1STaeung Song 	if (!item->name) {
65220105ca1STaeung Song 		pr_debug("%s: strdup failed\n", __func__);
65320105ca1STaeung Song 		free(item);
65420105ca1STaeung Song 		return NULL;
65520105ca1STaeung Song 	}
65620105ca1STaeung Song 
65720105ca1STaeung Song 	list_add_tail(&item->node, &section->items);
65820105ca1STaeung Song 	return item;
65920105ca1STaeung Song }
66020105ca1STaeung Song 
66120105ca1STaeung Song static int set_value(struct perf_config_item *item, const char *value)
66220105ca1STaeung Song {
66320105ca1STaeung Song 	char *val = strdup(value);
66420105ca1STaeung Song 
66520105ca1STaeung Song 	if (!val)
66620105ca1STaeung Song 		return -1;
66720105ca1STaeung Song 
66820105ca1STaeung Song 	zfree(&item->value);
66920105ca1STaeung Song 	item->value = val;
67020105ca1STaeung Song 	return 0;
67120105ca1STaeung Song }
67220105ca1STaeung Song 
67320105ca1STaeung Song static int collect_config(const char *var, const char *value,
67420105ca1STaeung Song 			  void *perf_config_set)
67520105ca1STaeung Song {
67620105ca1STaeung Song 	int ret = -1;
67720105ca1STaeung Song 	char *ptr, *key;
67820105ca1STaeung Song 	char *section_name, *name;
67920105ca1STaeung Song 	struct perf_config_section *section = NULL;
68020105ca1STaeung Song 	struct perf_config_item *item = NULL;
68120105ca1STaeung Song 	struct perf_config_set *set = perf_config_set;
6827db91f25STaeung Song 	struct list_head *sections;
68320105ca1STaeung Song 
6847db91f25STaeung Song 	if (set == NULL)
6857db91f25STaeung Song 		return -1;
6867db91f25STaeung Song 
6877db91f25STaeung Song 	sections = &set->sections;
68820105ca1STaeung Song 	key = ptr = strdup(var);
68920105ca1STaeung Song 	if (!key) {
69020105ca1STaeung Song 		pr_debug("%s: strdup failed\n", __func__);
69120105ca1STaeung Song 		return -1;
69220105ca1STaeung Song 	}
69320105ca1STaeung Song 
69420105ca1STaeung Song 	section_name = strsep(&ptr, ".");
69520105ca1STaeung Song 	name = ptr;
69620105ca1STaeung Song 	if (name == NULL || value == NULL)
69720105ca1STaeung Song 		goto out_free;
69820105ca1STaeung Song 
69920105ca1STaeung Song 	section = find_section(sections, section_name);
70020105ca1STaeung Song 	if (!section) {
70120105ca1STaeung Song 		section = add_section(sections, section_name);
70220105ca1STaeung Song 		if (!section)
70320105ca1STaeung Song 			goto out_free;
70420105ca1STaeung Song 	}
70520105ca1STaeung Song 
70620105ca1STaeung Song 	item = find_config_item(name, section);
70720105ca1STaeung Song 	if (!item) {
70820105ca1STaeung Song 		item = add_config_item(section, name);
70920105ca1STaeung Song 		if (!item)
71020105ca1STaeung Song 			goto out_free;
71120105ca1STaeung Song 	}
71220105ca1STaeung Song 
71308d090cfSTaeung Song 	/* perf_config_set can contain both user and system config items.
71408d090cfSTaeung Song 	 * So we should know where each value is from.
71508d090cfSTaeung Song 	 * The classification would be needed when a particular config file
7164d39c89fSIngo Molnar 	 * is overwritten by setting feature i.e. set_config().
71708d090cfSTaeung Song 	 */
71808d090cfSTaeung Song 	if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) {
71908d090cfSTaeung Song 		section->from_system_config = true;
72008d090cfSTaeung Song 		item->from_system_config = true;
72108d090cfSTaeung Song 	} else {
72208d090cfSTaeung Song 		section->from_system_config = false;
72308d090cfSTaeung Song 		item->from_system_config = false;
72408d090cfSTaeung Song 	}
72508d090cfSTaeung Song 
72620105ca1STaeung Song 	ret = set_value(item, value);
72720105ca1STaeung Song 
72820105ca1STaeung Song out_free:
72920105ca1STaeung Song 	free(key);
73054569ba4SChangbin Du 	return ret;
73120105ca1STaeung Song }
73220105ca1STaeung Song 
73308d090cfSTaeung Song int perf_config_set__collect(struct perf_config_set *set, const char *file_name,
734c6fc018aSTaeung Song 			     const char *var, const char *value)
735c6fc018aSTaeung Song {
73608d090cfSTaeung Song 	config_file_name = file_name;
737c6fc018aSTaeung Song 	return collect_config(var, value, set);
738c6fc018aSTaeung Song }
739c6fc018aSTaeung Song 
7408beeb00fSTaeung Song static int perf_config_set__init(struct perf_config_set *set)
7418beeb00fSTaeung Song {
7428beeb00fSTaeung Song 	int ret = -1;
7438beeb00fSTaeung Song 
7448beeb00fSTaeung Song 	/* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
7458beeb00fSTaeung Song 	if (config_exclusive_filename)
7468beeb00fSTaeung Song 		return perf_config_from_file(collect_config, config_exclusive_filename, set);
7478beeb00fSTaeung Song 	if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
7488beeb00fSTaeung Song 		if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0)
7498beeb00fSTaeung Song 			goto out;
7508beeb00fSTaeung Song 	}
751f5f03e19SJiri Olsa 	if (perf_config_global() && perf_home_perfconfig()) {
752f5f03e19SJiri Olsa 		if (perf_config_from_file(collect_config, perf_home_perfconfig(), set) < 0)
7538beeb00fSTaeung Song 			goto out;
7548beeb00fSTaeung Song 	}
7558beeb00fSTaeung Song 
7568beeb00fSTaeung Song out:
7578beeb00fSTaeung Song 	return ret;
7588beeb00fSTaeung Song }
7598beeb00fSTaeung Song 
76020105ca1STaeung Song struct perf_config_set *perf_config_set__new(void)
76120105ca1STaeung Song {
76220105ca1STaeung Song 	struct perf_config_set *set = zalloc(sizeof(*set));
76320105ca1STaeung Song 
76420105ca1STaeung Song 	if (set) {
76520105ca1STaeung Song 		INIT_LIST_HEAD(&set->sections);
76655421b4fSTaeung Song 		perf_config_set__init(set);
76720105ca1STaeung Song 	}
76820105ca1STaeung Song 
76920105ca1STaeung Song 	return set;
77020105ca1STaeung Song }
77120105ca1STaeung Song 
772a523026cSJiri Olsa struct perf_config_set *perf_config_set__load_file(const char *file)
773a523026cSJiri Olsa {
774a523026cSJiri Olsa 	struct perf_config_set *set = zalloc(sizeof(*set));
775a523026cSJiri Olsa 
776a523026cSJiri Olsa 	if (set) {
777a523026cSJiri Olsa 		INIT_LIST_HEAD(&set->sections);
778a523026cSJiri Olsa 		perf_config_from_file(collect_config, file, set);
779a523026cSJiri Olsa 	}
780a523026cSJiri Olsa 
781a523026cSJiri Olsa 	return set;
782a523026cSJiri Olsa }
783a523026cSJiri Olsa 
784d01bd1acSArnaldo Carvalho de Melo static int perf_config__init(void)
785d01bd1acSArnaldo Carvalho de Melo {
786d01bd1acSArnaldo Carvalho de Melo 	if (config_set == NULL)
787d01bd1acSArnaldo Carvalho de Melo 		config_set = perf_config_set__new();
788d01bd1acSArnaldo Carvalho de Melo 
789d01bd1acSArnaldo Carvalho de Melo 	return config_set == NULL;
790d01bd1acSArnaldo Carvalho de Melo }
791d01bd1acSArnaldo Carvalho de Melo 
792a523026cSJiri Olsa int perf_config_set(struct perf_config_set *set,
793a523026cSJiri Olsa 		    config_fn_t fn, void *data)
7948a0a9c7eSTaeung Song {
7958a0a9c7eSTaeung Song 	int ret = 0;
7968a0a9c7eSTaeung Song 	char key[BUFSIZ];
7978a0a9c7eSTaeung Song 	struct perf_config_section *section;
7988a0a9c7eSTaeung Song 	struct perf_config_item *item;
7998a0a9c7eSTaeung Song 
800a523026cSJiri Olsa 	perf_config_set__for_each_entry(set, section, item) {
8018a0a9c7eSTaeung Song 		char *value = item->value;
8028a0a9c7eSTaeung Song 
8038a0a9c7eSTaeung Song 		if (value) {
8048a0a9c7eSTaeung Song 			scnprintf(key, sizeof(key), "%s.%s",
8058a0a9c7eSTaeung Song 				  section->name, item->name);
8068a0a9c7eSTaeung Song 			ret = fn(key, value, data);
8078a0a9c7eSTaeung Song 			if (ret < 0) {
808a827c007SLike Xu 				pr_err("Error in the given config file: wrong config key-value pair %s=%s\n",
8098a0a9c7eSTaeung Song 				       key, value);
81022d46219SArnaldo Carvalho de Melo 				/*
81122d46219SArnaldo Carvalho de Melo 				 * Can't be just a 'break', as perf_config_set__for_each_entry()
81222d46219SArnaldo Carvalho de Melo 				 * expands to two nested for() loops.
81322d46219SArnaldo Carvalho de Melo 				 */
81422d46219SArnaldo Carvalho de Melo 				goto out;
8158a0a9c7eSTaeung Song 			}
8168a0a9c7eSTaeung Song 		}
8178a0a9c7eSTaeung Song 	}
81822d46219SArnaldo Carvalho de Melo out:
8198a0a9c7eSTaeung Song 	return ret;
8208a0a9c7eSTaeung Song }
8218a0a9c7eSTaeung Song 
822a523026cSJiri Olsa int perf_config(config_fn_t fn, void *data)
823a523026cSJiri Olsa {
824a523026cSJiri Olsa 	if (config_set == NULL && perf_config__init())
825a523026cSJiri Olsa 		return -1;
826a523026cSJiri Olsa 
827a523026cSJiri Olsa 	return perf_config_set(config_set, fn, data);
828a523026cSJiri Olsa }
829a523026cSJiri Olsa 
8308a0a9c7eSTaeung Song void perf_config__exit(void)
8318a0a9c7eSTaeung Song {
8328a0a9c7eSTaeung Song 	perf_config_set__delete(config_set);
8338a0a9c7eSTaeung Song 	config_set = NULL;
8348a0a9c7eSTaeung Song }
8358a0a9c7eSTaeung Song 
8368a0a9c7eSTaeung Song void perf_config__refresh(void)
8378a0a9c7eSTaeung Song {
8388a0a9c7eSTaeung Song 	perf_config__exit();
8398a0a9c7eSTaeung Song 	perf_config__init();
8408a0a9c7eSTaeung Song }
8418a0a9c7eSTaeung Song 
84220105ca1STaeung Song static void perf_config_item__delete(struct perf_config_item *item)
84320105ca1STaeung Song {
84420105ca1STaeung Song 	zfree(&item->name);
84520105ca1STaeung Song 	zfree(&item->value);
84620105ca1STaeung Song 	free(item);
84720105ca1STaeung Song }
84820105ca1STaeung Song 
84920105ca1STaeung Song static void perf_config_section__purge(struct perf_config_section *section)
85020105ca1STaeung Song {
85120105ca1STaeung Song 	struct perf_config_item *item, *tmp;
85220105ca1STaeung Song 
85320105ca1STaeung Song 	list_for_each_entry_safe(item, tmp, &section->items, node) {
85420105ca1STaeung Song 		list_del_init(&item->node);
85520105ca1STaeung Song 		perf_config_item__delete(item);
85620105ca1STaeung Song 	}
85720105ca1STaeung Song }
85820105ca1STaeung Song 
85920105ca1STaeung Song static void perf_config_section__delete(struct perf_config_section *section)
86020105ca1STaeung Song {
86120105ca1STaeung Song 	perf_config_section__purge(section);
86220105ca1STaeung Song 	zfree(&section->name);
86320105ca1STaeung Song 	free(section);
86420105ca1STaeung Song }
86520105ca1STaeung Song 
86620105ca1STaeung Song static void perf_config_set__purge(struct perf_config_set *set)
86720105ca1STaeung Song {
86820105ca1STaeung Song 	struct perf_config_section *section, *tmp;
86920105ca1STaeung Song 
87020105ca1STaeung Song 	list_for_each_entry_safe(section, tmp, &set->sections, node) {
87120105ca1STaeung Song 		list_del_init(&section->node);
87220105ca1STaeung Song 		perf_config_section__delete(section);
87320105ca1STaeung Song 	}
87420105ca1STaeung Song }
87520105ca1STaeung Song 
87620105ca1STaeung Song void perf_config_set__delete(struct perf_config_set *set)
87720105ca1STaeung Song {
878826424ccSTaeung Song 	if (set == NULL)
879826424ccSTaeung Song 		return;
880826424ccSTaeung Song 
88120105ca1STaeung Song 	perf_config_set__purge(set);
88220105ca1STaeung Song 	free(set);
88320105ca1STaeung Song }
88420105ca1STaeung Song 
88586470930SIngo Molnar /*
88686470930SIngo Molnar  * Call this to report error for your variable that should not
88786470930SIngo Molnar  * get a boolean value (i.e. "[my] var" means "true").
88886470930SIngo Molnar  */
88986470930SIngo Molnar int config_error_nonbool(const char *var)
89086470930SIngo Molnar {
89162d94b00SArnaldo Carvalho de Melo 	pr_err("Missing value for '%s'", var);
89262d94b00SArnaldo Carvalho de Melo 	return -1;
89386470930SIngo Molnar }
89445de34bbSStephane Eranian 
89599ce8e9fSJiri Olsa void set_buildid_dir(const char *dir)
89645de34bbSStephane Eranian {
89799ce8e9fSJiri Olsa 	if (dir)
89875c375c0SSihyeon Jang 		scnprintf(buildid_dir, MAXPATHLEN, "%s", dir);
89945de34bbSStephane Eranian 
90045de34bbSStephane Eranian 	/* default to $HOME/.debug */
90145de34bbSStephane Eranian 	if (buildid_dir[0] == '\0') {
90237194f44STaeung Song 		char *home = getenv("HOME");
90337194f44STaeung Song 
90437194f44STaeung Song 		if (home) {
90575c375c0SSihyeon Jang 			snprintf(buildid_dir, MAXPATHLEN, "%s/%s",
90637194f44STaeung Song 				 home, DEBUG_CACHE_DIR);
90745de34bbSStephane Eranian 		} else {
90845de34bbSStephane Eranian 			strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
90945de34bbSStephane Eranian 		}
91045de34bbSStephane Eranian 		buildid_dir[MAXPATHLEN-1] = '\0';
91145de34bbSStephane Eranian 	}
91245de34bbSStephane Eranian 	/* for communicating with external commands */
91345de34bbSStephane Eranian 	setenv("PERF_BUILDID_DIR", buildid_dir, 1);
91445de34bbSStephane Eranian }
915c3ca8d44SAdrian Hunter 
916c3ca8d44SAdrian Hunter struct perf_config_scan_data {
917c3ca8d44SAdrian Hunter 	const char *name;
918c3ca8d44SAdrian Hunter 	const char *fmt;
919c3ca8d44SAdrian Hunter 	va_list args;
920c3ca8d44SAdrian Hunter 	int ret;
921c3ca8d44SAdrian Hunter };
922c3ca8d44SAdrian Hunter 
923c3ca8d44SAdrian Hunter static int perf_config_scan_cb(const char *var, const char *value, void *data)
924c3ca8d44SAdrian Hunter {
925c3ca8d44SAdrian Hunter 	struct perf_config_scan_data *d = data;
926c3ca8d44SAdrian Hunter 
927c3ca8d44SAdrian Hunter 	if (!strcmp(var, d->name))
928c3ca8d44SAdrian Hunter 		d->ret = vsscanf(value, d->fmt, d->args);
929c3ca8d44SAdrian Hunter 
930c3ca8d44SAdrian Hunter 	return 0;
931c3ca8d44SAdrian Hunter }
932c3ca8d44SAdrian Hunter 
933c3ca8d44SAdrian Hunter int perf_config_scan(const char *name, const char *fmt, ...)
934c3ca8d44SAdrian Hunter {
935c3ca8d44SAdrian Hunter 	struct perf_config_scan_data d = {
936c3ca8d44SAdrian Hunter 		.name = name,
937c3ca8d44SAdrian Hunter 		.fmt = fmt,
938c3ca8d44SAdrian Hunter 	};
939c3ca8d44SAdrian Hunter 
940c3ca8d44SAdrian Hunter 	va_start(d.args, fmt);
941c3ca8d44SAdrian Hunter 	perf_config(perf_config_scan_cb, &d);
942c3ca8d44SAdrian Hunter 	va_end(d.args);
943c3ca8d44SAdrian Hunter 
944c3ca8d44SAdrian Hunter 	return d.ret;
945c3ca8d44SAdrian Hunter }
946