123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- /*
- * config.c
- *
- * Helper functions for parsing config items.
- * Originally copied from GIT source.
- *
- * Copyright (C) Linus Torvalds, 2005
- * Copyright (C) Johannes Schindelin, 2005
- *
- */
- #include "util.h"
- #include "cache.h"
- #include "exec_cmd.h"
- #include "util/hist.h" /* perf_hist_config */
- #include "util/llvm-utils.h" /* perf_llvm_config */
- #define MAXNAME (256)
- #define DEBUG_CACHE_DIR ".debug"
- char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
- static FILE *config_file;
- static const char *config_file_name;
- static int config_linenr;
- static int config_file_eof;
- static const char *config_exclusive_filename;
- static int get_next_char(void)
- {
- int c;
- FILE *f;
- c = '\n';
- if ((f = config_file) != NULL) {
- c = fgetc(f);
- if (c == '\r') {
- /* DOS like systems */
- c = fgetc(f);
- if (c != '\n') {
- ungetc(c, f);
- c = '\r';
- }
- }
- if (c == '\n')
- config_linenr++;
- if (c == EOF) {
- config_file_eof = 1;
- c = '\n';
- }
- }
- return c;
- }
- static char *parse_value(void)
- {
- static char value[1024];
- int quote = 0, comment = 0, space = 0;
- size_t len = 0;
- for (;;) {
- int c = get_next_char();
- if (len >= sizeof(value) - 1)
- return NULL;
- if (c == '\n') {
- if (quote)
- return NULL;
- value[len] = 0;
- return value;
- }
- if (comment)
- continue;
- if (isspace(c) && !quote) {
- space = 1;
- continue;
- }
- if (!quote) {
- if (c == ';' || c == '#') {
- comment = 1;
- continue;
- }
- }
- if (space) {
- if (len)
- value[len++] = ' ';
- space = 0;
- }
- if (c == '\\') {
- c = get_next_char();
- switch (c) {
- case '\n':
- continue;
- case 't':
- c = '\t';
- break;
- case 'b':
- c = '\b';
- break;
- case 'n':
- c = '\n';
- break;
- /* Some characters escape as themselves */
- case '\\': case '"':
- break;
- /* Reject unknown escape sequences */
- default:
- return NULL;
- }
- value[len++] = c;
- continue;
- }
- if (c == '"') {
- quote = 1-quote;
- continue;
- }
- value[len++] = c;
- }
- }
- static inline int iskeychar(int c)
- {
- return isalnum(c) || c == '-' || c == '_';
- }
- static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
- {
- int c;
- char *value;
- /* Get the full name */
- for (;;) {
- c = get_next_char();
- if (config_file_eof)
- break;
- if (!iskeychar(c))
- break;
- name[len++] = c;
- if (len >= MAXNAME)
- return -1;
- }
- name[len] = 0;
- while (c == ' ' || c == '\t')
- c = get_next_char();
- value = NULL;
- if (c != '\n') {
- if (c != '=')
- return -1;
- value = parse_value();
- if (!value)
- return -1;
- }
- return fn(name, value, data);
- }
- static int get_extended_base_var(char *name, int baselen, int c)
- {
- do {
- if (c == '\n')
- return -1;
- c = get_next_char();
- } while (isspace(c));
- /* We require the format to be '[base "extension"]' */
- if (c != '"')
- return -1;
- name[baselen++] = '.';
- for (;;) {
- int ch = get_next_char();
- if (ch == '\n')
- return -1;
- if (ch == '"')
- break;
- if (ch == '\\') {
- ch = get_next_char();
- if (ch == '\n')
- return -1;
- }
- name[baselen++] = ch;
- if (baselen > MAXNAME / 2)
- return -1;
- }
- /* Final ']' */
- if (get_next_char() != ']')
- return -1;
- return baselen;
- }
- static int get_base_var(char *name)
- {
- int baselen = 0;
- for (;;) {
- int c = get_next_char();
- if (config_file_eof)
- return -1;
- if (c == ']')
- return baselen;
- if (isspace(c))
- return get_extended_base_var(name, baselen, c);
- if (!iskeychar(c) && c != '.')
- return -1;
- if (baselen > MAXNAME / 2)
- return -1;
- name[baselen++] = tolower(c);
- }
- }
- static int perf_parse_file(config_fn_t fn, void *data)
- {
- int comment = 0;
- int baselen = 0;
- static char var[MAXNAME];
- /* U+FEFF Byte Order Mark in UTF8 */
- static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
- const unsigned char *bomptr = utf8_bom;
- for (;;) {
- int line, c = get_next_char();
- if (bomptr && *bomptr) {
- /* We are at the file beginning; skip UTF8-encoded BOM
- * if present. Sane editors won't put this in on their
- * own, but e.g. Windows Notepad will do it happily. */
- if ((unsigned char) c == *bomptr) {
- bomptr++;
- continue;
- } else {
- /* Do not tolerate partial BOM. */
- if (bomptr != utf8_bom)
- break;
- /* No BOM at file beginning. Cool. */
- bomptr = NULL;
- }
- }
- if (c == '\n') {
- if (config_file_eof)
- return 0;
- comment = 0;
- continue;
- }
- if (comment || isspace(c))
- continue;
- if (c == '#' || c == ';') {
- comment = 1;
- continue;
- }
- if (c == '[') {
- baselen = get_base_var(var);
- if (baselen <= 0)
- break;
- var[baselen++] = '.';
- var[baselen] = 0;
- continue;
- }
- if (!isalpha(c))
- break;
- var[baselen] = tolower(c);
- /*
- * The get_value function might or might not reach the '\n',
- * so saving the current line number for error reporting.
- */
- line = config_linenr;
- if (get_value(fn, data, var, baselen+1) < 0) {
- config_linenr = line;
- break;
- }
- }
- die("bad config file line %d in %s", config_linenr, config_file_name);
- }
- static int parse_unit_factor(const char *end, unsigned long *val)
- {
- if (!*end)
- return 1;
- else if (!strcasecmp(end, "k")) {
- *val *= 1024;
- return 1;
- }
- else if (!strcasecmp(end, "m")) {
- *val *= 1024 * 1024;
- return 1;
- }
- else if (!strcasecmp(end, "g")) {
- *val *= 1024 * 1024 * 1024;
- return 1;
- }
- return 0;
- }
- static int perf_parse_llong(const char *value, long long *ret)
- {
- if (value && *value) {
- char *end;
- long long val = strtoll(value, &end, 0);
- unsigned long factor = 1;
- if (!parse_unit_factor(end, &factor))
- return 0;
- *ret = val * factor;
- return 1;
- }
- return 0;
- }
- static int perf_parse_long(const char *value, long *ret)
- {
- if (value && *value) {
- char *end;
- long val = strtol(value, &end, 0);
- unsigned long factor = 1;
- if (!parse_unit_factor(end, &factor))
- return 0;
- *ret = val * factor;
- return 1;
- }
- return 0;
- }
- static void die_bad_config(const char *name)
- {
- if (config_file_name)
- die("bad config value for '%s' in %s", name, config_file_name);
- die("bad config value for '%s'", name);
- }
- u64 perf_config_u64(const char *name, const char *value)
- {
- long long ret = 0;
- if (!perf_parse_llong(value, &ret))
- die_bad_config(name);
- return (u64) ret;
- }
- int perf_config_int(const char *name, const char *value)
- {
- long ret = 0;
- if (!perf_parse_long(value, &ret))
- die_bad_config(name);
- return ret;
- }
- static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
- {
- *is_bool = 1;
- if (!value)
- return 1;
- if (!*value)
- return 0;
- if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
- return 1;
- if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
- return 0;
- *is_bool = 0;
- return perf_config_int(name, value);
- }
- int perf_config_bool(const char *name, const char *value)
- {
- int discard;
- return !!perf_config_bool_or_int(name, value, &discard);
- }
- const char *perf_config_dirname(const char *name, const char *value)
- {
- if (!name)
- return NULL;
- return value;
- }
- static int perf_default_core_config(const char *var __maybe_unused,
- const char *value __maybe_unused)
- {
- /* Add other config variables here. */
- return 0;
- }
- static int perf_ui_config(const char *var, const char *value)
- {
- /* Add other config variables here. */
- if (!strcmp(var, "ui.show-headers")) {
- symbol_conf.show_hist_headers = perf_config_bool(var, value);
- return 0;
- }
- return 0;
- }
- int perf_default_config(const char *var, const char *value,
- void *dummy __maybe_unused)
- {
- if (!prefixcmp(var, "core."))
- return perf_default_core_config(var, value);
- if (!prefixcmp(var, "hist."))
- return perf_hist_config(var, value);
- if (!prefixcmp(var, "ui."))
- return perf_ui_config(var, value);
- if (!prefixcmp(var, "call-graph."))
- return perf_callchain_config(var, value);
- if (!prefixcmp(var, "llvm."))
- return perf_llvm_config(var, value);
- /* Add other config variables here. */
- return 0;
- }
- static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
- {
- int ret;
- FILE *f = fopen(filename, "r");
- ret = -1;
- if (f) {
- config_file = f;
- config_file_name = filename;
- config_linenr = 1;
- config_file_eof = 0;
- ret = perf_parse_file(fn, data);
- fclose(f);
- config_file_name = NULL;
- }
- return ret;
- }
- static const char *perf_etc_perfconfig(void)
- {
- static const char *system_wide;
- if (!system_wide)
- system_wide = system_path(ETC_PERFCONFIG);
- return system_wide;
- }
- static int perf_env_bool(const char *k, int def)
- {
- const char *v = getenv(k);
- return v ? perf_config_bool(k, v) : def;
- }
- static int perf_config_system(void)
- {
- return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
- }
- static int perf_config_global(void)
- {
- return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
- }
- int perf_config(config_fn_t fn, void *data)
- {
- int ret = 0, found = 0;
- const char *home = NULL;
- /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
- if (config_exclusive_filename)
- return perf_config_from_file(fn, config_exclusive_filename, data);
- if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
- ret += perf_config_from_file(fn, perf_etc_perfconfig(),
- data);
- found += 1;
- }
- home = getenv("HOME");
- if (perf_config_global() && home) {
- char *user_config = strdup(mkpath("%s/.perfconfig", home));
- struct stat st;
- if (user_config == NULL) {
- warning("Not enough memory to process %s/.perfconfig, "
- "ignoring it.", home);
- goto out;
- }
- if (stat(user_config, &st) < 0)
- goto out_free;
- if (st.st_uid && (st.st_uid != geteuid())) {
- warning("File %s not owned by current user or root, "
- "ignoring it.", user_config);
- goto out_free;
- }
- if (!st.st_size)
- goto out_free;
- ret += perf_config_from_file(fn, user_config, data);
- found += 1;
- out_free:
- free(user_config);
- }
- out:
- if (found == 0)
- return -1;
- return ret;
- }
- /*
- * Call this to report error for your variable that should not
- * get a boolean value (i.e. "[my] var" means "true").
- */
- int config_error_nonbool(const char *var)
- {
- return error("Missing value for '%s'", var);
- }
- struct buildid_dir_config {
- char *dir;
- };
- static int buildid_dir_command_config(const char *var, const char *value,
- void *data)
- {
- struct buildid_dir_config *c = data;
- const char *v;
- /* same dir for all commands */
- if (!strcmp(var, "buildid.dir")) {
- v = perf_config_dirname(var, value);
- if (!v)
- return -1;
- strncpy(c->dir, v, MAXPATHLEN-1);
- c->dir[MAXPATHLEN-1] = '\0';
- }
- return 0;
- }
- static void check_buildid_dir_config(void)
- {
- struct buildid_dir_config c;
- c.dir = buildid_dir;
- perf_config(buildid_dir_command_config, &c);
- }
- void set_buildid_dir(const char *dir)
- {
- if (dir)
- scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir);
- /* try config file */
- if (buildid_dir[0] == '\0')
- check_buildid_dir_config();
- /* default to $HOME/.debug */
- if (buildid_dir[0] == '\0') {
- char *v = getenv("HOME");
- if (v) {
- snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
- v, DEBUG_CACHE_DIR);
- } else {
- strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
- }
- buildid_dir[MAXPATHLEN-1] = '\0';
- }
- /* for communicating with external commands */
- setenv("PERF_BUILDID_DIR", buildid_dir, 1);
- }
|