Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / config.c
blob7e3c1b60120c259a7b01f32cf1e109e4af873b89
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * config.c
5 * Helper functions for parsing config items.
6 * Originally copied from GIT source.
8 * Copyright (C) Linus Torvalds, 2005
9 * Copyright (C) Johannes Schindelin, 2005
12 #include <errno.h>
13 #include <sys/param.h>
14 #include "util.h"
15 #include "cache.h"
16 #include "callchain.h"
17 #include <subcmd/exec-cmd.h>
18 #include "util/event.h" /* proc_map_timeout */
19 #include "util/hist.h" /* perf_hist_config */
20 #include "util/llvm-utils.h" /* perf_llvm_config */
21 #include "config.h"
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <linux/string.h>
27 #include "sane_ctype.h"
29 #define MAXNAME (256)
31 #define DEBUG_CACHE_DIR ".debug"
34 char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
36 static FILE *config_file;
37 static const char *config_file_name;
38 static int config_linenr;
39 static int config_file_eof;
40 static struct perf_config_set *config_set;
42 const char *config_exclusive_filename;
44 static int get_next_char(void)
46 int c;
47 FILE *f;
49 c = '\n';
50 if ((f = config_file) != NULL) {
51 c = fgetc(f);
52 if (c == '\r') {
53 /* DOS like systems */
54 c = fgetc(f);
55 if (c != '\n') {
56 ungetc(c, f);
57 c = '\r';
60 if (c == '\n')
61 config_linenr++;
62 if (c == EOF) {
63 config_file_eof = 1;
64 c = '\n';
67 return c;
70 static char *parse_value(void)
72 static char value[1024];
73 int quote = 0, comment = 0, space = 0;
74 size_t len = 0;
76 for (;;) {
77 int c = get_next_char();
79 if (len >= sizeof(value) - 1)
80 return NULL;
81 if (c == '\n') {
82 if (quote)
83 return NULL;
84 value[len] = 0;
85 return value;
87 if (comment)
88 continue;
89 if (isspace(c) && !quote) {
90 space = 1;
91 continue;
93 if (!quote) {
94 if (c == ';' || c == '#') {
95 comment = 1;
96 continue;
99 if (space) {
100 if (len)
101 value[len++] = ' ';
102 space = 0;
104 if (c == '\\') {
105 c = get_next_char();
106 switch (c) {
107 case '\n':
108 continue;
109 case 't':
110 c = '\t';
111 break;
112 case 'b':
113 c = '\b';
114 break;
115 case 'n':
116 c = '\n';
117 break;
118 /* Some characters escape as themselves */
119 case '\\': case '"':
120 break;
121 /* Reject unknown escape sequences */
122 default:
123 return NULL;
125 value[len++] = c;
126 continue;
128 if (c == '"') {
129 quote = 1-quote;
130 continue;
132 value[len++] = c;
136 static inline int iskeychar(int c)
138 return isalnum(c) || c == '-' || c == '_';
141 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
143 int c;
144 char *value;
146 /* Get the full name */
147 for (;;) {
148 c = get_next_char();
149 if (config_file_eof)
150 break;
151 if (!iskeychar(c))
152 break;
153 name[len++] = c;
154 if (len >= MAXNAME)
155 return -1;
157 name[len] = 0;
158 while (c == ' ' || c == '\t')
159 c = get_next_char();
161 value = NULL;
162 if (c != '\n') {
163 if (c != '=')
164 return -1;
165 value = parse_value();
166 if (!value)
167 return -1;
169 return fn(name, value, data);
172 static int get_extended_base_var(char *name, int baselen, int c)
174 do {
175 if (c == '\n')
176 return -1;
177 c = get_next_char();
178 } while (isspace(c));
180 /* We require the format to be '[base "extension"]' */
181 if (c != '"')
182 return -1;
183 name[baselen++] = '.';
185 for (;;) {
186 int ch = get_next_char();
188 if (ch == '\n')
189 return -1;
190 if (ch == '"')
191 break;
192 if (ch == '\\') {
193 ch = get_next_char();
194 if (ch == '\n')
195 return -1;
197 name[baselen++] = ch;
198 if (baselen > MAXNAME / 2)
199 return -1;
202 /* Final ']' */
203 if (get_next_char() != ']')
204 return -1;
205 return baselen;
208 static int get_base_var(char *name)
210 int baselen = 0;
212 for (;;) {
213 int c = get_next_char();
214 if (config_file_eof)
215 return -1;
216 if (c == ']')
217 return baselen;
218 if (isspace(c))
219 return get_extended_base_var(name, baselen, c);
220 if (!iskeychar(c) && c != '.')
221 return -1;
222 if (baselen > MAXNAME / 2)
223 return -1;
224 name[baselen++] = tolower(c);
228 static int perf_parse_file(config_fn_t fn, void *data)
230 int comment = 0;
231 int baselen = 0;
232 static char var[MAXNAME];
234 /* U+FEFF Byte Order Mark in UTF8 */
235 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
236 const unsigned char *bomptr = utf8_bom;
238 for (;;) {
239 int line, c = get_next_char();
241 if (bomptr && *bomptr) {
242 /* We are at the file beginning; skip UTF8-encoded BOM
243 * if present. Sane editors won't put this in on their
244 * own, but e.g. Windows Notepad will do it happily. */
245 if ((unsigned char) c == *bomptr) {
246 bomptr++;
247 continue;
248 } else {
249 /* Do not tolerate partial BOM. */
250 if (bomptr != utf8_bom)
251 break;
252 /* No BOM at file beginning. Cool. */
253 bomptr = NULL;
256 if (c == '\n') {
257 if (config_file_eof)
258 return 0;
259 comment = 0;
260 continue;
262 if (comment || isspace(c))
263 continue;
264 if (c == '#' || c == ';') {
265 comment = 1;
266 continue;
268 if (c == '[') {
269 baselen = get_base_var(var);
270 if (baselen <= 0)
271 break;
272 var[baselen++] = '.';
273 var[baselen] = 0;
274 continue;
276 if (!isalpha(c))
277 break;
278 var[baselen] = tolower(c);
281 * The get_value function might or might not reach the '\n',
282 * so saving the current line number for error reporting.
284 line = config_linenr;
285 if (get_value(fn, data, var, baselen+1) < 0) {
286 config_linenr = line;
287 break;
290 pr_err("bad config file line %d in %s\n", config_linenr, config_file_name);
291 return -1;
294 static int parse_unit_factor(const char *end, unsigned long *val)
296 if (!*end)
297 return 1;
298 else if (!strcasecmp(end, "k")) {
299 *val *= 1024;
300 return 1;
302 else if (!strcasecmp(end, "m")) {
303 *val *= 1024 * 1024;
304 return 1;
306 else if (!strcasecmp(end, "g")) {
307 *val *= 1024 * 1024 * 1024;
308 return 1;
310 return 0;
313 static int perf_parse_llong(const char *value, long long *ret)
315 if (value && *value) {
316 char *end;
317 long long val = strtoll(value, &end, 0);
318 unsigned long factor = 1;
320 if (!parse_unit_factor(end, &factor))
321 return 0;
322 *ret = val * factor;
323 return 1;
325 return 0;
328 static int perf_parse_long(const char *value, long *ret)
330 if (value && *value) {
331 char *end;
332 long val = strtol(value, &end, 0);
333 unsigned long factor = 1;
334 if (!parse_unit_factor(end, &factor))
335 return 0;
336 *ret = val * factor;
337 return 1;
339 return 0;
342 static void bad_config(const char *name)
344 if (config_file_name)
345 pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name);
346 else
347 pr_warning("bad config value for '%s', ignoring...\n", name);
350 int perf_config_u64(u64 *dest, const char *name, const char *value)
352 long long ret = 0;
354 if (!perf_parse_llong(value, &ret)) {
355 bad_config(name);
356 return -1;
359 *dest = ret;
360 return 0;
363 int perf_config_int(int *dest, const char *name, const char *value)
365 long ret = 0;
366 if (!perf_parse_long(value, &ret)) {
367 bad_config(name);
368 return -1;
370 *dest = ret;
371 return 0;
374 static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
376 int ret;
378 *is_bool = 1;
379 if (!value)
380 return 1;
381 if (!*value)
382 return 0;
383 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
384 return 1;
385 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
386 return 0;
387 *is_bool = 0;
388 return perf_config_int(&ret, name, value) < 0 ? -1 : ret;
391 int perf_config_bool(const char *name, const char *value)
393 int discard;
394 return !!perf_config_bool_or_int(name, value, &discard);
397 static const char *perf_config_dirname(const char *name, const char *value)
399 if (!name)
400 return NULL;
401 return value;
404 static int perf_buildid_config(const char *var, const char *value)
406 /* same dir for all commands */
407 if (!strcmp(var, "buildid.dir")) {
408 const char *dir = perf_config_dirname(var, value);
410 if (!dir) {
411 pr_err("Invalid buildid directory!\n");
412 return -1;
414 strncpy(buildid_dir, dir, MAXPATHLEN-1);
415 buildid_dir[MAXPATHLEN-1] = '\0';
418 return 0;
421 static int perf_default_core_config(const char *var __maybe_unused,
422 const char *value __maybe_unused)
424 if (!strcmp(var, "core.proc-map-timeout"))
425 proc_map_timeout = strtoul(value, NULL, 10);
427 /* Add other config variables here. */
428 return 0;
431 static int perf_ui_config(const char *var, const char *value)
433 /* Add other config variables here. */
434 if (!strcmp(var, "ui.show-headers"))
435 symbol_conf.show_hist_headers = perf_config_bool(var, value);
437 return 0;
440 int perf_default_config(const char *var, const char *value,
441 void *dummy __maybe_unused)
443 if (strstarts(var, "core."))
444 return perf_default_core_config(var, value);
446 if (strstarts(var, "hist."))
447 return perf_hist_config(var, value);
449 if (strstarts(var, "ui."))
450 return perf_ui_config(var, value);
452 if (strstarts(var, "call-graph."))
453 return perf_callchain_config(var, value);
455 if (strstarts(var, "llvm."))
456 return perf_llvm_config(var, value);
458 if (strstarts(var, "buildid."))
459 return perf_buildid_config(var, value);
461 /* Add other config variables here. */
462 return 0;
465 static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
467 int ret;
468 FILE *f = fopen(filename, "r");
470 ret = -1;
471 if (f) {
472 config_file = f;
473 config_file_name = filename;
474 config_linenr = 1;
475 config_file_eof = 0;
476 ret = perf_parse_file(fn, data);
477 fclose(f);
478 config_file_name = NULL;
480 return ret;
483 const char *perf_etc_perfconfig(void)
485 static const char *system_wide;
486 if (!system_wide)
487 system_wide = system_path(ETC_PERFCONFIG);
488 return system_wide;
491 static int perf_env_bool(const char *k, int def)
493 const char *v = getenv(k);
494 return v ? perf_config_bool(k, v) : def;
497 static int perf_config_system(void)
499 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
502 static int perf_config_global(void)
504 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
507 static struct perf_config_section *find_section(struct list_head *sections,
508 const char *section_name)
510 struct perf_config_section *section;
512 list_for_each_entry(section, sections, node)
513 if (!strcmp(section->name, section_name))
514 return section;
516 return NULL;
519 static struct perf_config_item *find_config_item(const char *name,
520 struct perf_config_section *section)
522 struct perf_config_item *item;
524 list_for_each_entry(item, &section->items, node)
525 if (!strcmp(item->name, name))
526 return item;
528 return NULL;
531 static struct perf_config_section *add_section(struct list_head *sections,
532 const char *section_name)
534 struct perf_config_section *section = zalloc(sizeof(*section));
536 if (!section)
537 return NULL;
539 INIT_LIST_HEAD(&section->items);
540 section->name = strdup(section_name);
541 if (!section->name) {
542 pr_debug("%s: strdup failed\n", __func__);
543 free(section);
544 return NULL;
547 list_add_tail(&section->node, sections);
548 return section;
551 static struct perf_config_item *add_config_item(struct perf_config_section *section,
552 const char *name)
554 struct perf_config_item *item = zalloc(sizeof(*item));
556 if (!item)
557 return NULL;
559 item->name = strdup(name);
560 if (!item->name) {
561 pr_debug("%s: strdup failed\n", __func__);
562 free(item);
563 return NULL;
566 list_add_tail(&item->node, &section->items);
567 return item;
570 static int set_value(struct perf_config_item *item, const char *value)
572 char *val = strdup(value);
574 if (!val)
575 return -1;
577 zfree(&item->value);
578 item->value = val;
579 return 0;
582 static int collect_config(const char *var, const char *value,
583 void *perf_config_set)
585 int ret = -1;
586 char *ptr, *key;
587 char *section_name, *name;
588 struct perf_config_section *section = NULL;
589 struct perf_config_item *item = NULL;
590 struct perf_config_set *set = perf_config_set;
591 struct list_head *sections;
593 if (set == NULL)
594 return -1;
596 sections = &set->sections;
597 key = ptr = strdup(var);
598 if (!key) {
599 pr_debug("%s: strdup failed\n", __func__);
600 return -1;
603 section_name = strsep(&ptr, ".");
604 name = ptr;
605 if (name == NULL || value == NULL)
606 goto out_free;
608 section = find_section(sections, section_name);
609 if (!section) {
610 section = add_section(sections, section_name);
611 if (!section)
612 goto out_free;
615 item = find_config_item(name, section);
616 if (!item) {
617 item = add_config_item(section, name);
618 if (!item)
619 goto out_free;
622 /* perf_config_set can contain both user and system config items.
623 * So we should know where each value is from.
624 * The classification would be needed when a particular config file
625 * is overwrited by setting feature i.e. set_config().
627 if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) {
628 section->from_system_config = true;
629 item->from_system_config = true;
630 } else {
631 section->from_system_config = false;
632 item->from_system_config = false;
635 ret = set_value(item, value);
637 out_free:
638 free(key);
639 return ret;
642 int perf_config_set__collect(struct perf_config_set *set, const char *file_name,
643 const char *var, const char *value)
645 config_file_name = file_name;
646 return collect_config(var, value, set);
649 static int perf_config_set__init(struct perf_config_set *set)
651 int ret = -1;
652 const char *home = NULL;
653 char *user_config;
654 struct stat st;
656 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
657 if (config_exclusive_filename)
658 return perf_config_from_file(collect_config, config_exclusive_filename, set);
659 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
660 if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0)
661 goto out;
664 home = getenv("HOME");
667 * Skip reading user config if:
668 * - there is no place to read it from (HOME)
669 * - we are asked not to (PERF_CONFIG_NOGLOBAL=1)
671 if (!home || !*home || !perf_config_global())
672 return 0;
674 user_config = strdup(mkpath("%s/.perfconfig", home));
675 if (user_config == NULL) {
676 pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home);
677 goto out;
680 if (stat(user_config, &st) < 0) {
681 if (errno == ENOENT)
682 ret = 0;
683 goto out_free;
686 ret = 0;
688 if (st.st_uid && (st.st_uid != geteuid())) {
689 pr_warning("File %s not owned by current user or root, ignoring it.", user_config);
690 goto out_free;
693 if (st.st_size)
694 ret = perf_config_from_file(collect_config, user_config, set);
696 out_free:
697 free(user_config);
698 out:
699 return ret;
702 struct perf_config_set *perf_config_set__new(void)
704 struct perf_config_set *set = zalloc(sizeof(*set));
706 if (set) {
707 INIT_LIST_HEAD(&set->sections);
708 perf_config_set__init(set);
711 return set;
714 static int perf_config__init(void)
716 if (config_set == NULL)
717 config_set = perf_config_set__new();
719 return config_set == NULL;
722 int perf_config(config_fn_t fn, void *data)
724 int ret = 0;
725 char key[BUFSIZ];
726 struct perf_config_section *section;
727 struct perf_config_item *item;
729 if (config_set == NULL && perf_config__init())
730 return -1;
732 perf_config_set__for_each_entry(config_set, section, item) {
733 char *value = item->value;
735 if (value) {
736 scnprintf(key, sizeof(key), "%s.%s",
737 section->name, item->name);
738 ret = fn(key, value, data);
739 if (ret < 0) {
740 pr_err("Error: wrong config key-value pair %s=%s\n",
741 key, value);
742 break;
747 return ret;
750 void perf_config__exit(void)
752 perf_config_set__delete(config_set);
753 config_set = NULL;
756 void perf_config__refresh(void)
758 perf_config__exit();
759 perf_config__init();
762 static void perf_config_item__delete(struct perf_config_item *item)
764 zfree(&item->name);
765 zfree(&item->value);
766 free(item);
769 static void perf_config_section__purge(struct perf_config_section *section)
771 struct perf_config_item *item, *tmp;
773 list_for_each_entry_safe(item, tmp, &section->items, node) {
774 list_del_init(&item->node);
775 perf_config_item__delete(item);
779 static void perf_config_section__delete(struct perf_config_section *section)
781 perf_config_section__purge(section);
782 zfree(&section->name);
783 free(section);
786 static void perf_config_set__purge(struct perf_config_set *set)
788 struct perf_config_section *section, *tmp;
790 list_for_each_entry_safe(section, tmp, &set->sections, node) {
791 list_del_init(&section->node);
792 perf_config_section__delete(section);
796 void perf_config_set__delete(struct perf_config_set *set)
798 if (set == NULL)
799 return;
801 perf_config_set__purge(set);
802 free(set);
806 * Call this to report error for your variable that should not
807 * get a boolean value (i.e. "[my] var" means "true").
809 int config_error_nonbool(const char *var)
811 pr_err("Missing value for '%s'", var);
812 return -1;
815 void set_buildid_dir(const char *dir)
817 if (dir)
818 scnprintf(buildid_dir, MAXPATHLEN, "%s", dir);
820 /* default to $HOME/.debug */
821 if (buildid_dir[0] == '\0') {
822 char *home = getenv("HOME");
824 if (home) {
825 snprintf(buildid_dir, MAXPATHLEN, "%s/%s",
826 home, DEBUG_CACHE_DIR);
827 } else {
828 strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
830 buildid_dir[MAXPATHLEN-1] = '\0';
832 /* for communicating with external commands */
833 setenv("PERF_BUILDID_DIR", buildid_dir, 1);