4 * Helper functions for parsing config items.
5 * Originally copied from GIT source.
7 * Copyright (C) Linus Torvalds, 2005
8 * Copyright (C) Johannes Schindelin, 2005
13 #include <subcmd/exec-cmd.h>
14 #include "util/hist.h" /* perf_hist_config */
15 #include "util/llvm-utils.h" /* perf_llvm_config */
20 #define DEBUG_CACHE_DIR ".debug"
23 char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
25 static FILE *config_file;
26 static const char *config_file_name;
27 static int config_linenr;
28 static int config_file_eof;
30 const char *config_exclusive_filename;
32 static int get_next_char(void)
38 if ((f = config_file) != NULL) {
41 /* DOS like systems */
58 static char *parse_value(void)
60 static char value[1024];
61 int quote = 0, comment = 0, space = 0;
65 int c = get_next_char();
67 if (len >= sizeof(value) - 1)
77 if (isspace(c) && !quote) {
82 if (c == ';' || c == '#') {
106 /* Some characters escape as themselves */
109 /* Reject unknown escape sequences */
124 static inline int iskeychar(int c)
126 return isalnum(c) || c == '-' || c == '_';
129 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
134 /* Get the full name */
146 while (c == ' ' || c == '\t')
153 value = parse_value();
157 return fn(name, value, data);
160 static int get_extended_base_var(char *name, int baselen, int c)
166 } while (isspace(c));
168 /* We require the format to be '[base "extension"]' */
171 name[baselen++] = '.';
174 int ch = get_next_char();
181 ch = get_next_char();
185 name[baselen++] = ch;
186 if (baselen > MAXNAME / 2)
191 if (get_next_char() != ']')
196 static int get_base_var(char *name)
201 int c = get_next_char();
207 return get_extended_base_var(name, baselen, c);
208 if (!iskeychar(c) && c != '.')
210 if (baselen > MAXNAME / 2)
212 name[baselen++] = tolower(c);
216 static int perf_parse_file(config_fn_t fn, void *data)
220 static char var[MAXNAME];
222 /* U+FEFF Byte Order Mark in UTF8 */
223 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
224 const unsigned char *bomptr = utf8_bom;
227 int line, c = get_next_char();
229 if (bomptr && *bomptr) {
230 /* We are at the file beginning; skip UTF8-encoded BOM
231 * if present. Sane editors won't put this in on their
232 * own, but e.g. Windows Notepad will do it happily. */
233 if ((unsigned char) c == *bomptr) {
237 /* Do not tolerate partial BOM. */
238 if (bomptr != utf8_bom)
240 /* No BOM at file beginning. Cool. */
250 if (comment || isspace(c))
252 if (c == '#' || c == ';') {
257 baselen = get_base_var(var);
260 var[baselen++] = '.';
266 var[baselen] = tolower(c);
269 * The get_value function might or might not reach the '\n',
270 * so saving the current line number for error reporting.
272 line = config_linenr;
273 if (get_value(fn, data, var, baselen+1) < 0) {
274 config_linenr = line;
278 die("bad config file line %d in %s", config_linenr, config_file_name);
281 static int parse_unit_factor(const char *end, unsigned long *val)
285 else if (!strcasecmp(end, "k")) {
289 else if (!strcasecmp(end, "m")) {
293 else if (!strcasecmp(end, "g")) {
294 *val *= 1024 * 1024 * 1024;
300 static int perf_parse_llong(const char *value, long long *ret)
302 if (value && *value) {
304 long long val = strtoll(value, &end, 0);
305 unsigned long factor = 1;
307 if (!parse_unit_factor(end, &factor))
315 static int perf_parse_long(const char *value, long *ret)
317 if (value && *value) {
319 long val = strtol(value, &end, 0);
320 unsigned long factor = 1;
321 if (!parse_unit_factor(end, &factor))
329 static void die_bad_config(const char *name)
331 if (config_file_name)
332 die("bad config value for '%s' in %s", name, config_file_name);
333 die("bad config value for '%s'", name);
336 u64 perf_config_u64(const char *name, const char *value)
340 if (!perf_parse_llong(value, &ret))
341 die_bad_config(name);
345 int perf_config_int(const char *name, const char *value)
348 if (!perf_parse_long(value, &ret))
349 die_bad_config(name);
353 static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
360 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
362 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
365 return perf_config_int(name, value);
368 int perf_config_bool(const char *name, const char *value)
371 return !!perf_config_bool_or_int(name, value, &discard);
374 const char *perf_config_dirname(const char *name, const char *value)
381 static int perf_buildid_config(const char *var, const char *value)
383 /* same dir for all commands */
384 if (!strcmp(var, "buildid.dir")) {
385 const char *dir = perf_config_dirname(var, value);
389 strncpy(buildid_dir, dir, MAXPATHLEN-1);
390 buildid_dir[MAXPATHLEN-1] = '\0';
396 static int perf_default_core_config(const char *var __maybe_unused,
397 const char *value __maybe_unused)
399 /* Add other config variables here. */
403 static int perf_ui_config(const char *var, const char *value)
405 /* Add other config variables here. */
406 if (!strcmp(var, "ui.show-headers")) {
407 symbol_conf.show_hist_headers = perf_config_bool(var, value);
413 int perf_default_config(const char *var, const char *value,
414 void *dummy __maybe_unused)
416 if (!prefixcmp(var, "core."))
417 return perf_default_core_config(var, value);
419 if (!prefixcmp(var, "hist."))
420 return perf_hist_config(var, value);
422 if (!prefixcmp(var, "ui."))
423 return perf_ui_config(var, value);
425 if (!prefixcmp(var, "call-graph."))
426 return perf_callchain_config(var, value);
428 if (!prefixcmp(var, "llvm."))
429 return perf_llvm_config(var, value);
431 if (!prefixcmp(var, "buildid."))
432 return perf_buildid_config(var, value);
434 /* Add other config variables here. */
438 static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
441 FILE *f = fopen(filename, "r");
446 config_file_name = filename;
449 ret = perf_parse_file(fn, data);
451 config_file_name = NULL;
456 const char *perf_etc_perfconfig(void)
458 static const char *system_wide;
460 system_wide = system_path(ETC_PERFCONFIG);
464 static int perf_env_bool(const char *k, int def)
466 const char *v = getenv(k);
467 return v ? perf_config_bool(k, v) : def;
470 static int perf_config_system(void)
472 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
475 static int perf_config_global(void)
477 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
480 int perf_config(config_fn_t fn, void *data)
482 int ret = 0, found = 0;
483 const char *home = NULL;
485 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
486 if (config_exclusive_filename)
487 return perf_config_from_file(fn, config_exclusive_filename, data);
488 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
489 ret += perf_config_from_file(fn, perf_etc_perfconfig(),
494 home = getenv("HOME");
495 if (perf_config_global() && home) {
496 char *user_config = strdup(mkpath("%s/.perfconfig", home));
499 if (user_config == NULL) {
500 warning("Not enough memory to process %s/.perfconfig, "
501 "ignoring it.", home);
505 if (stat(user_config, &st) < 0)
508 if (st.st_uid && (st.st_uid != geteuid())) {
509 warning("File %s not owned by current user or root, "
510 "ignoring it.", user_config);
517 ret += perf_config_from_file(fn, user_config, data);
528 static struct perf_config_section *find_section(struct list_head *sections,
529 const char *section_name)
531 struct perf_config_section *section;
533 list_for_each_entry(section, sections, node)
534 if (!strcmp(section->name, section_name))
540 static struct perf_config_item *find_config_item(const char *name,
541 struct perf_config_section *section)
543 struct perf_config_item *item;
545 list_for_each_entry(item, §ion->items, node)
546 if (!strcmp(item->name, name))
552 static struct perf_config_section *add_section(struct list_head *sections,
553 const char *section_name)
555 struct perf_config_section *section = zalloc(sizeof(*section));
560 INIT_LIST_HEAD(§ion->items);
561 section->name = strdup(section_name);
562 if (!section->name) {
563 pr_debug("%s: strdup failed\n", __func__);
568 list_add_tail(§ion->node, sections);
572 static struct perf_config_item *add_config_item(struct perf_config_section *section,
575 struct perf_config_item *item = zalloc(sizeof(*item));
580 item->name = strdup(name);
582 pr_debug("%s: strdup failed\n", __func__);
587 list_add_tail(&item->node, §ion->items);
591 static int set_value(struct perf_config_item *item, const char *value)
593 char *val = strdup(value);
603 static int collect_config(const char *var, const char *value,
604 void *perf_config_set)
608 char *section_name, *name;
609 struct perf_config_section *section = NULL;
610 struct perf_config_item *item = NULL;
611 struct perf_config_set *set = perf_config_set;
612 struct list_head *sections = &set->sections;
614 key = ptr = strdup(var);
616 pr_debug("%s: strdup failed\n", __func__);
620 section_name = strsep(&ptr, ".");
622 if (name == NULL || value == NULL)
625 section = find_section(sections, section_name);
627 section = add_section(sections, section_name);
632 item = find_config_item(name, section);
634 item = add_config_item(section, name);
639 ret = set_value(item, value);
644 perf_config_set__delete(set);
648 struct perf_config_set *perf_config_set__new(void)
650 struct perf_config_set *set = zalloc(sizeof(*set));
653 INIT_LIST_HEAD(&set->sections);
654 perf_config(collect_config, set);
660 static void perf_config_item__delete(struct perf_config_item *item)
667 static void perf_config_section__purge(struct perf_config_section *section)
669 struct perf_config_item *item, *tmp;
671 list_for_each_entry_safe(item, tmp, §ion->items, node) {
672 list_del_init(&item->node);
673 perf_config_item__delete(item);
677 static void perf_config_section__delete(struct perf_config_section *section)
679 perf_config_section__purge(section);
680 zfree(§ion->name);
684 static void perf_config_set__purge(struct perf_config_set *set)
686 struct perf_config_section *section, *tmp;
688 list_for_each_entry_safe(section, tmp, &set->sections, node) {
689 list_del_init(§ion->node);
690 perf_config_section__delete(section);
694 void perf_config_set__delete(struct perf_config_set *set)
696 perf_config_set__purge(set);
701 * Call this to report error for your variable that should not
702 * get a boolean value (i.e. "[my] var" means "true").
704 int config_error_nonbool(const char *var)
706 return error("Missing value for '%s'", var);
709 void set_buildid_dir(const char *dir)
712 scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir);
714 /* default to $HOME/.debug */
715 if (buildid_dir[0] == '\0') {
716 char *home = getenv("HOME");
719 snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
720 home, DEBUG_CACHE_DIR);
722 strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
724 buildid_dir[MAXPATHLEN-1] = '\0';
726 /* for communicating with external commands */
727 setenv("PERF_BUILDID_DIR", buildid_dir, 1);