xref: /openbmc/linux/tools/perf/util/config.c (revision 0b93da1756df4fe930ee0220a6addce263a6e0ab)
186470930SIngo Molnar /*
25f9273d6SNamhyung Kim  * config.c
35f9273d6SNamhyung Kim  *
45f9273d6SNamhyung Kim  * Helper functions for parsing config items.
55f9273d6SNamhyung Kim  * Originally copied from GIT source.
686470930SIngo Molnar  *
786470930SIngo Molnar  * Copyright (C) Linus Torvalds, 2005
886470930SIngo Molnar  * Copyright (C) Johannes Schindelin, 2005
986470930SIngo Molnar  *
1086470930SIngo Molnar  */
1186470930SIngo Molnar #include "util.h"
1286470930SIngo Molnar #include "cache.h"
1386470930SIngo Molnar #include "exec_cmd.h"
14*0b93da17SNamhyung Kim #include "util/hist.h"  /* perf_hist_config */
1586470930SIngo Molnar 
1686470930SIngo Molnar #define MAXNAME (256)
1786470930SIngo Molnar 
1845de34bbSStephane Eranian #define DEBUG_CACHE_DIR ".debug"
1945de34bbSStephane Eranian 
2045de34bbSStephane Eranian 
2145de34bbSStephane Eranian char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
2245de34bbSStephane Eranian 
2386470930SIngo Molnar static FILE *config_file;
2486470930SIngo Molnar static const char *config_file_name;
2586470930SIngo Molnar static int config_linenr;
2686470930SIngo Molnar static int config_file_eof;
2786470930SIngo Molnar 
28a41794cdSArnaldo Carvalho de Melo static const char *config_exclusive_filename;
2986470930SIngo Molnar 
3086470930SIngo Molnar static int get_next_char(void)
3186470930SIngo Molnar {
3286470930SIngo Molnar 	int c;
3386470930SIngo Molnar 	FILE *f;
3486470930SIngo Molnar 
3586470930SIngo Molnar 	c = '\n';
3686470930SIngo Molnar 	if ((f = config_file) != NULL) {
3786470930SIngo Molnar 		c = fgetc(f);
3886470930SIngo Molnar 		if (c == '\r') {
3986470930SIngo Molnar 			/* DOS like systems */
4086470930SIngo Molnar 			c = fgetc(f);
4186470930SIngo Molnar 			if (c != '\n') {
4286470930SIngo Molnar 				ungetc(c, f);
4386470930SIngo Molnar 				c = '\r';
4486470930SIngo Molnar 			}
4586470930SIngo Molnar 		}
4686470930SIngo Molnar 		if (c == '\n')
4786470930SIngo Molnar 			config_linenr++;
4886470930SIngo Molnar 		if (c == EOF) {
4986470930SIngo Molnar 			config_file_eof = 1;
5086470930SIngo Molnar 			c = '\n';
5186470930SIngo Molnar 		}
5286470930SIngo Molnar 	}
5386470930SIngo Molnar 	return c;
5486470930SIngo Molnar }
5586470930SIngo Molnar 
5686470930SIngo Molnar static char *parse_value(void)
5786470930SIngo Molnar {
5886470930SIngo Molnar 	static char value[1024];
59f37a291cSIngo Molnar 	int quote = 0, comment = 0, space = 0;
60f37a291cSIngo Molnar 	size_t len = 0;
6186470930SIngo Molnar 
6286470930SIngo Molnar 	for (;;) {
6386470930SIngo Molnar 		int c = get_next_char();
64f37a291cSIngo Molnar 
6586470930SIngo Molnar 		if (len >= sizeof(value) - 1)
6686470930SIngo Molnar 			return NULL;
6786470930SIngo Molnar 		if (c == '\n') {
6886470930SIngo Molnar 			if (quote)
6986470930SIngo Molnar 				return NULL;
7086470930SIngo Molnar 			value[len] = 0;
7186470930SIngo Molnar 			return value;
7286470930SIngo Molnar 		}
7386470930SIngo Molnar 		if (comment)
7486470930SIngo Molnar 			continue;
7586470930SIngo Molnar 		if (isspace(c) && !quote) {
7686470930SIngo Molnar 			space = 1;
7786470930SIngo Molnar 			continue;
7886470930SIngo Molnar 		}
7986470930SIngo Molnar 		if (!quote) {
8086470930SIngo Molnar 			if (c == ';' || c == '#') {
8186470930SIngo Molnar 				comment = 1;
8286470930SIngo Molnar 				continue;
8386470930SIngo Molnar 			}
8486470930SIngo Molnar 		}
8586470930SIngo Molnar 		if (space) {
8686470930SIngo Molnar 			if (len)
8786470930SIngo Molnar 				value[len++] = ' ';
8886470930SIngo Molnar 			space = 0;
8986470930SIngo Molnar 		}
9086470930SIngo Molnar 		if (c == '\\') {
9186470930SIngo Molnar 			c = get_next_char();
9286470930SIngo Molnar 			switch (c) {
9386470930SIngo Molnar 			case '\n':
9486470930SIngo Molnar 				continue;
9586470930SIngo Molnar 			case 't':
9686470930SIngo Molnar 				c = '\t';
9786470930SIngo Molnar 				break;
9886470930SIngo Molnar 			case 'b':
9986470930SIngo Molnar 				c = '\b';
10086470930SIngo Molnar 				break;
10186470930SIngo Molnar 			case 'n':
10286470930SIngo Molnar 				c = '\n';
10386470930SIngo Molnar 				break;
10486470930SIngo Molnar 			/* Some characters escape as themselves */
10586470930SIngo Molnar 			case '\\': case '"':
10686470930SIngo Molnar 				break;
10786470930SIngo Molnar 			/* Reject unknown escape sequences */
10886470930SIngo Molnar 			default:
10986470930SIngo Molnar 				return NULL;
11086470930SIngo Molnar 			}
11186470930SIngo Molnar 			value[len++] = c;
11286470930SIngo Molnar 			continue;
11386470930SIngo Molnar 		}
11486470930SIngo Molnar 		if (c == '"') {
11586470930SIngo Molnar 			quote = 1-quote;
11686470930SIngo Molnar 			continue;
11786470930SIngo Molnar 		}
11886470930SIngo Molnar 		value[len++] = c;
11986470930SIngo Molnar 	}
12086470930SIngo Molnar }
12186470930SIngo Molnar 
12286470930SIngo Molnar static inline int iskeychar(int c)
12386470930SIngo Molnar {
1248dc7c651SArnaldo Carvalho de Melo 	return isalnum(c) || c == '-' || c == '_';
12586470930SIngo Molnar }
12686470930SIngo Molnar 
12786470930SIngo Molnar static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
12886470930SIngo Molnar {
12986470930SIngo Molnar 	int c;
13086470930SIngo Molnar 	char *value;
13186470930SIngo Molnar 
13286470930SIngo Molnar 	/* Get the full name */
13386470930SIngo Molnar 	for (;;) {
13486470930SIngo Molnar 		c = get_next_char();
13586470930SIngo Molnar 		if (config_file_eof)
13686470930SIngo Molnar 			break;
13786470930SIngo Molnar 		if (!iskeychar(c))
13886470930SIngo Molnar 			break;
13945de34bbSStephane Eranian 		name[len++] = c;
14086470930SIngo Molnar 		if (len >= MAXNAME)
14186470930SIngo Molnar 			return -1;
14286470930SIngo Molnar 	}
14386470930SIngo Molnar 	name[len] = 0;
14486470930SIngo Molnar 	while (c == ' ' || c == '\t')
14586470930SIngo Molnar 		c = get_next_char();
14686470930SIngo Molnar 
14786470930SIngo Molnar 	value = NULL;
14886470930SIngo Molnar 	if (c != '\n') {
14986470930SIngo Molnar 		if (c != '=')
15086470930SIngo Molnar 			return -1;
15186470930SIngo Molnar 		value = parse_value();
15286470930SIngo Molnar 		if (!value)
15386470930SIngo Molnar 			return -1;
15486470930SIngo Molnar 	}
15586470930SIngo Molnar 	return fn(name, value, data);
15686470930SIngo Molnar }
15786470930SIngo Molnar 
15886470930SIngo Molnar static int get_extended_base_var(char *name, int baselen, int c)
15986470930SIngo Molnar {
16086470930SIngo Molnar 	do {
16186470930SIngo Molnar 		if (c == '\n')
16286470930SIngo Molnar 			return -1;
16386470930SIngo Molnar 		c = get_next_char();
16486470930SIngo Molnar 	} while (isspace(c));
16586470930SIngo Molnar 
16686470930SIngo Molnar 	/* We require the format to be '[base "extension"]' */
16786470930SIngo Molnar 	if (c != '"')
16886470930SIngo Molnar 		return -1;
16986470930SIngo Molnar 	name[baselen++] = '.';
17086470930SIngo Molnar 
17186470930SIngo Molnar 	for (;;) {
17283a0944fSIngo Molnar 		int ch = get_next_char();
17383a0944fSIngo Molnar 
17483a0944fSIngo Molnar 		if (ch == '\n')
17586470930SIngo Molnar 			return -1;
17683a0944fSIngo Molnar 		if (ch == '"')
17786470930SIngo Molnar 			break;
17883a0944fSIngo Molnar 		if (ch == '\\') {
17983a0944fSIngo Molnar 			ch = get_next_char();
18083a0944fSIngo Molnar 			if (ch == '\n')
18186470930SIngo Molnar 				return -1;
18286470930SIngo Molnar 		}
18383a0944fSIngo Molnar 		name[baselen++] = ch;
18486470930SIngo Molnar 		if (baselen > MAXNAME / 2)
18586470930SIngo Molnar 			return -1;
18686470930SIngo Molnar 	}
18786470930SIngo Molnar 
18886470930SIngo Molnar 	/* Final ']' */
18986470930SIngo Molnar 	if (get_next_char() != ']')
19086470930SIngo Molnar 		return -1;
19186470930SIngo Molnar 	return baselen;
19286470930SIngo Molnar }
19386470930SIngo Molnar 
19486470930SIngo Molnar static int get_base_var(char *name)
19586470930SIngo Molnar {
19686470930SIngo Molnar 	int baselen = 0;
19786470930SIngo Molnar 
19886470930SIngo Molnar 	for (;;) {
19986470930SIngo Molnar 		int c = get_next_char();
20086470930SIngo Molnar 		if (config_file_eof)
20186470930SIngo Molnar 			return -1;
20286470930SIngo Molnar 		if (c == ']')
20386470930SIngo Molnar 			return baselen;
20486470930SIngo Molnar 		if (isspace(c))
20586470930SIngo Molnar 			return get_extended_base_var(name, baselen, c);
20686470930SIngo Molnar 		if (!iskeychar(c) && c != '.')
20786470930SIngo Molnar 			return -1;
20886470930SIngo Molnar 		if (baselen > MAXNAME / 2)
20986470930SIngo Molnar 			return -1;
21086470930SIngo Molnar 		name[baselen++] = tolower(c);
21186470930SIngo Molnar 	}
21286470930SIngo Molnar }
21386470930SIngo Molnar 
21486470930SIngo Molnar static int perf_parse_file(config_fn_t fn, void *data)
21586470930SIngo Molnar {
21686470930SIngo Molnar 	int comment = 0;
21786470930SIngo Molnar 	int baselen = 0;
21886470930SIngo Molnar 	static char var[MAXNAME];
21986470930SIngo Molnar 
22086470930SIngo Molnar 	/* U+FEFF Byte Order Mark in UTF8 */
22186470930SIngo Molnar 	static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
22286470930SIngo Molnar 	const unsigned char *bomptr = utf8_bom;
22386470930SIngo Molnar 
22486470930SIngo Molnar 	for (;;) {
22586470930SIngo Molnar 		int c = get_next_char();
22686470930SIngo Molnar 		if (bomptr && *bomptr) {
22786470930SIngo Molnar 			/* We are at the file beginning; skip UTF8-encoded BOM
22886470930SIngo Molnar 			 * if present. Sane editors won't put this in on their
22986470930SIngo Molnar 			 * own, but e.g. Windows Notepad will do it happily. */
23086470930SIngo Molnar 			if ((unsigned char) c == *bomptr) {
23186470930SIngo Molnar 				bomptr++;
23286470930SIngo Molnar 				continue;
23386470930SIngo Molnar 			} else {
23486470930SIngo Molnar 				/* Do not tolerate partial BOM. */
23586470930SIngo Molnar 				if (bomptr != utf8_bom)
23686470930SIngo Molnar 					break;
23786470930SIngo Molnar 				/* No BOM at file beginning. Cool. */
23886470930SIngo Molnar 				bomptr = NULL;
23986470930SIngo Molnar 			}
24086470930SIngo Molnar 		}
24186470930SIngo Molnar 		if (c == '\n') {
24286470930SIngo Molnar 			if (config_file_eof)
24386470930SIngo Molnar 				return 0;
24486470930SIngo Molnar 			comment = 0;
24586470930SIngo Molnar 			continue;
24686470930SIngo Molnar 		}
24786470930SIngo Molnar 		if (comment || isspace(c))
24886470930SIngo Molnar 			continue;
24986470930SIngo Molnar 		if (c == '#' || c == ';') {
25086470930SIngo Molnar 			comment = 1;
25186470930SIngo Molnar 			continue;
25286470930SIngo Molnar 		}
25386470930SIngo Molnar 		if (c == '[') {
25486470930SIngo Molnar 			baselen = get_base_var(var);
25586470930SIngo Molnar 			if (baselen <= 0)
25686470930SIngo Molnar 				break;
25786470930SIngo Molnar 			var[baselen++] = '.';
25886470930SIngo Molnar 			var[baselen] = 0;
25986470930SIngo Molnar 			continue;
26086470930SIngo Molnar 		}
26186470930SIngo Molnar 		if (!isalpha(c))
26286470930SIngo Molnar 			break;
26386470930SIngo Molnar 		var[baselen] = tolower(c);
26486470930SIngo Molnar 		if (get_value(fn, data, var, baselen+1) < 0)
26586470930SIngo Molnar 			break;
26686470930SIngo Molnar 	}
26786470930SIngo Molnar 	die("bad config file line %d in %s", config_linenr, config_file_name);
26886470930SIngo Molnar }
26986470930SIngo Molnar 
27086470930SIngo Molnar static int parse_unit_factor(const char *end, unsigned long *val)
27186470930SIngo Molnar {
27286470930SIngo Molnar 	if (!*end)
27386470930SIngo Molnar 		return 1;
27486470930SIngo Molnar 	else if (!strcasecmp(end, "k")) {
27586470930SIngo Molnar 		*val *= 1024;
27686470930SIngo Molnar 		return 1;
27786470930SIngo Molnar 	}
27886470930SIngo Molnar 	else if (!strcasecmp(end, "m")) {
27986470930SIngo Molnar 		*val *= 1024 * 1024;
28086470930SIngo Molnar 		return 1;
28186470930SIngo Molnar 	}
28286470930SIngo Molnar 	else if (!strcasecmp(end, "g")) {
28386470930SIngo Molnar 		*val *= 1024 * 1024 * 1024;
28486470930SIngo Molnar 		return 1;
28586470930SIngo Molnar 	}
28686470930SIngo Molnar 	return 0;
28786470930SIngo Molnar }
28886470930SIngo Molnar 
28986470930SIngo Molnar static int perf_parse_long(const char *value, long *ret)
29086470930SIngo Molnar {
29186470930SIngo Molnar 	if (value && *value) {
29286470930SIngo Molnar 		char *end;
29386470930SIngo Molnar 		long val = strtol(value, &end, 0);
29486470930SIngo Molnar 		unsigned long factor = 1;
29586470930SIngo Molnar 		if (!parse_unit_factor(end, &factor))
29686470930SIngo Molnar 			return 0;
29786470930SIngo Molnar 		*ret = val * factor;
29886470930SIngo Molnar 		return 1;
29986470930SIngo Molnar 	}
30086470930SIngo Molnar 	return 0;
30186470930SIngo Molnar }
30286470930SIngo Molnar 
30386470930SIngo Molnar static void die_bad_config(const char *name)
30486470930SIngo Molnar {
30586470930SIngo Molnar 	if (config_file_name)
30686470930SIngo Molnar 		die("bad config value for '%s' in %s", name, config_file_name);
30786470930SIngo Molnar 	die("bad config value for '%s'", name);
30886470930SIngo Molnar }
30986470930SIngo Molnar 
31086470930SIngo Molnar int perf_config_int(const char *name, const char *value)
31186470930SIngo Molnar {
31286470930SIngo Molnar 	long ret = 0;
31386470930SIngo Molnar 	if (!perf_parse_long(value, &ret))
31486470930SIngo Molnar 		die_bad_config(name);
31586470930SIngo Molnar 	return ret;
31686470930SIngo Molnar }
31786470930SIngo Molnar 
318a41794cdSArnaldo Carvalho de Melo static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
31986470930SIngo Molnar {
32086470930SIngo Molnar 	*is_bool = 1;
32186470930SIngo Molnar 	if (!value)
32286470930SIngo Molnar 		return 1;
32386470930SIngo Molnar 	if (!*value)
32486470930SIngo Molnar 		return 0;
32586470930SIngo Molnar 	if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
32686470930SIngo Molnar 		return 1;
32786470930SIngo Molnar 	if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
32886470930SIngo Molnar 		return 0;
32986470930SIngo Molnar 	*is_bool = 0;
33086470930SIngo Molnar 	return perf_config_int(name, value);
33186470930SIngo Molnar }
33286470930SIngo Molnar 
33386470930SIngo Molnar int perf_config_bool(const char *name, const char *value)
33486470930SIngo Molnar {
33586470930SIngo Molnar 	int discard;
33686470930SIngo Molnar 	return !!perf_config_bool_or_int(name, value, &discard);
33786470930SIngo Molnar }
33886470930SIngo Molnar 
33945de34bbSStephane Eranian const char *perf_config_dirname(const char *name, const char *value)
34045de34bbSStephane Eranian {
34145de34bbSStephane Eranian 	if (!name)
34245de34bbSStephane Eranian 		return NULL;
34345de34bbSStephane Eranian 	return value;
34445de34bbSStephane Eranian }
34545de34bbSStephane Eranian 
3461d037ca1SIrina Tirdea static int perf_default_core_config(const char *var __maybe_unused,
3471d037ca1SIrina Tirdea 				    const char *value __maybe_unused)
34886470930SIngo Molnar {
349395cf969SPaul Bolle 	/* Add other config variables here. */
35086470930SIngo Molnar 	return 0;
35186470930SIngo Molnar }
35286470930SIngo Molnar 
3531d037ca1SIrina Tirdea int perf_default_config(const char *var, const char *value,
3541d037ca1SIrina Tirdea 			void *dummy __maybe_unused)
35586470930SIngo Molnar {
35686470930SIngo Molnar 	if (!prefixcmp(var, "core."))
35786470930SIngo Molnar 		return perf_default_core_config(var, value);
35886470930SIngo Molnar 
359*0b93da17SNamhyung Kim 	if (!prefixcmp(var, "hist."))
360*0b93da17SNamhyung Kim 		return perf_hist_config(var, value);
361*0b93da17SNamhyung Kim 
362395cf969SPaul Bolle 	/* Add other config variables here. */
36386470930SIngo Molnar 	return 0;
36486470930SIngo Molnar }
36586470930SIngo Molnar 
366a41794cdSArnaldo Carvalho de Melo static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
36786470930SIngo Molnar {
36886470930SIngo Molnar 	int ret;
36986470930SIngo Molnar 	FILE *f = fopen(filename, "r");
37086470930SIngo Molnar 
37186470930SIngo Molnar 	ret = -1;
37286470930SIngo Molnar 	if (f) {
37386470930SIngo Molnar 		config_file = f;
37486470930SIngo Molnar 		config_file_name = filename;
37586470930SIngo Molnar 		config_linenr = 1;
37686470930SIngo Molnar 		config_file_eof = 0;
37786470930SIngo Molnar 		ret = perf_parse_file(fn, data);
37886470930SIngo Molnar 		fclose(f);
37986470930SIngo Molnar 		config_file_name = NULL;
38086470930SIngo Molnar 	}
38186470930SIngo Molnar 	return ret;
38286470930SIngo Molnar }
38386470930SIngo Molnar 
384a41794cdSArnaldo Carvalho de Melo static const char *perf_etc_perfconfig(void)
38586470930SIngo Molnar {
38686470930SIngo Molnar 	static const char *system_wide;
38786470930SIngo Molnar 	if (!system_wide)
38886470930SIngo Molnar 		system_wide = system_path(ETC_PERFCONFIG);
38986470930SIngo Molnar 	return system_wide;
39086470930SIngo Molnar }
39186470930SIngo Molnar 
39286470930SIngo Molnar static int perf_env_bool(const char *k, int def)
39386470930SIngo Molnar {
39486470930SIngo Molnar 	const char *v = getenv(k);
39586470930SIngo Molnar 	return v ? perf_config_bool(k, v) : def;
39686470930SIngo Molnar }
39786470930SIngo Molnar 
398a41794cdSArnaldo Carvalho de Melo static int perf_config_system(void)
39986470930SIngo Molnar {
40086470930SIngo Molnar 	return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
40186470930SIngo Molnar }
40286470930SIngo Molnar 
403a41794cdSArnaldo Carvalho de Melo static int perf_config_global(void)
40486470930SIngo Molnar {
40586470930SIngo Molnar 	return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
40686470930SIngo Molnar }
40786470930SIngo Molnar 
40886470930SIngo Molnar int perf_config(config_fn_t fn, void *data)
40986470930SIngo Molnar {
41086470930SIngo Molnar 	int ret = 0, found = 0;
41186470930SIngo Molnar 	const char *home = NULL;
41286470930SIngo Molnar 
41386470930SIngo Molnar 	/* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
41486470930SIngo Molnar 	if (config_exclusive_filename)
41586470930SIngo Molnar 		return perf_config_from_file(fn, config_exclusive_filename, data);
41686470930SIngo Molnar 	if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
41786470930SIngo Molnar 		ret += perf_config_from_file(fn, perf_etc_perfconfig(),
41886470930SIngo Molnar 					    data);
41986470930SIngo Molnar 		found += 1;
42086470930SIngo Molnar 	}
42186470930SIngo Molnar 
42286470930SIngo Molnar 	home = getenv("HOME");
42386470930SIngo Molnar 	if (perf_config_global() && home) {
42486470930SIngo Molnar 		char *user_config = strdup(mkpath("%s/.perfconfig", home));
425069e3725SArnaldo Carvalho de Melo 		struct stat st;
426069e3725SArnaldo Carvalho de Melo 
427069e3725SArnaldo Carvalho de Melo 		if (user_config == NULL) {
428069e3725SArnaldo Carvalho de Melo 			warning("Not enough memory to process %s/.perfconfig, "
429069e3725SArnaldo Carvalho de Melo 				"ignoring it.", home);
430069e3725SArnaldo Carvalho de Melo 			goto out;
43186470930SIngo Molnar 		}
43286470930SIngo Molnar 
433069e3725SArnaldo Carvalho de Melo 		if (stat(user_config, &st) < 0)
434069e3725SArnaldo Carvalho de Melo 			goto out_free;
435069e3725SArnaldo Carvalho de Melo 
436069e3725SArnaldo Carvalho de Melo 		if (st.st_uid && (st.st_uid != geteuid())) {
437069e3725SArnaldo Carvalho de Melo 			warning("File %s not owned by current user or root, "
438069e3725SArnaldo Carvalho de Melo 				"ignoring it.", user_config);
439069e3725SArnaldo Carvalho de Melo 			goto out_free;
440069e3725SArnaldo Carvalho de Melo 		}
441069e3725SArnaldo Carvalho de Melo 
442069e3725SArnaldo Carvalho de Melo 		if (!st.st_size)
443069e3725SArnaldo Carvalho de Melo 			goto out_free;
444069e3725SArnaldo Carvalho de Melo 
445069e3725SArnaldo Carvalho de Melo 		ret += perf_config_from_file(fn, user_config, data);
446069e3725SArnaldo Carvalho de Melo 		found += 1;
447069e3725SArnaldo Carvalho de Melo out_free:
448069e3725SArnaldo Carvalho de Melo 		free(user_config);
449069e3725SArnaldo Carvalho de Melo 	}
450069e3725SArnaldo Carvalho de Melo out:
45186470930SIngo Molnar 	if (found == 0)
45286470930SIngo Molnar 		return -1;
45386470930SIngo Molnar 	return ret;
45486470930SIngo Molnar }
45586470930SIngo Molnar 
45686470930SIngo Molnar /*
45786470930SIngo Molnar  * Call this to report error for your variable that should not
45886470930SIngo Molnar  * get a boolean value (i.e. "[my] var" means "true").
45986470930SIngo Molnar  */
46086470930SIngo Molnar int config_error_nonbool(const char *var)
46186470930SIngo Molnar {
46286470930SIngo Molnar 	return error("Missing value for '%s'", var);
46386470930SIngo Molnar }
46445de34bbSStephane Eranian 
46545de34bbSStephane Eranian struct buildid_dir_config {
46645de34bbSStephane Eranian 	char *dir;
46745de34bbSStephane Eranian };
46845de34bbSStephane Eranian 
46945de34bbSStephane Eranian static int buildid_dir_command_config(const char *var, const char *value,
47045de34bbSStephane Eranian 				      void *data)
47145de34bbSStephane Eranian {
47245de34bbSStephane Eranian 	struct buildid_dir_config *c = data;
47345de34bbSStephane Eranian 	const char *v;
47445de34bbSStephane Eranian 
47545de34bbSStephane Eranian 	/* same dir for all commands */
47645de34bbSStephane Eranian 	if (!prefixcmp(var, "buildid.") && !strcmp(var + 8, "dir")) {
47745de34bbSStephane Eranian 		v = perf_config_dirname(var, value);
47845de34bbSStephane Eranian 		if (!v)
47945de34bbSStephane Eranian 			return -1;
48045de34bbSStephane Eranian 		strncpy(c->dir, v, MAXPATHLEN-1);
48145de34bbSStephane Eranian 		c->dir[MAXPATHLEN-1] = '\0';
48245de34bbSStephane Eranian 	}
48345de34bbSStephane Eranian 	return 0;
48445de34bbSStephane Eranian }
48545de34bbSStephane Eranian 
48645de34bbSStephane Eranian static void check_buildid_dir_config(void)
48745de34bbSStephane Eranian {
48845de34bbSStephane Eranian 	struct buildid_dir_config c;
48945de34bbSStephane Eranian 	c.dir = buildid_dir;
49045de34bbSStephane Eranian 	perf_config(buildid_dir_command_config, &c);
49145de34bbSStephane Eranian }
49245de34bbSStephane Eranian 
49345de34bbSStephane Eranian void set_buildid_dir(void)
49445de34bbSStephane Eranian {
49545de34bbSStephane Eranian 	buildid_dir[0] = '\0';
49645de34bbSStephane Eranian 
49745de34bbSStephane Eranian 	/* try config file */
49845de34bbSStephane Eranian 	check_buildid_dir_config();
49945de34bbSStephane Eranian 
50045de34bbSStephane Eranian 	/* default to $HOME/.debug */
50145de34bbSStephane Eranian 	if (buildid_dir[0] == '\0') {
50245de34bbSStephane Eranian 		char *v = getenv("HOME");
50345de34bbSStephane Eranian 		if (v) {
50445de34bbSStephane Eranian 			snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
50545de34bbSStephane Eranian 				 v, DEBUG_CACHE_DIR);
50645de34bbSStephane Eranian 		} else {
50745de34bbSStephane Eranian 			strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
50845de34bbSStephane Eranian 		}
50945de34bbSStephane Eranian 		buildid_dir[MAXPATHLEN-1] = '\0';
51045de34bbSStephane Eranian 	}
51145de34bbSStephane Eranian 	/* for communicating with external commands */
51245de34bbSStephane Eranian 	setenv("PERF_BUILDID_DIR", buildid_dir, 1);
51345de34bbSStephane Eranian }
514