1 // SPDX-License-Identifier: GPL-2.0
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
13 #include <sys/param.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 */
22 #include <sys/types.h>
25 #include <linux/string.h>
27 #include "sane_ctype.h"
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)
50 if ((f
= config_file
) != NULL
) {
53 /* DOS like systems */
70 static char *parse_value(void)
72 static char value
[1024];
73 int quote
= 0, comment
= 0, space
= 0;
77 int c
= get_next_char();
79 if (len
>= sizeof(value
) - 1)
89 if (isspace(c
) && !quote
) {
94 if (c
== ';' || c
== '#') {
118 /* Some characters escape as themselves */
121 /* Reject unknown escape sequences */
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
)
146 /* Get the full name */
158 while (c
== ' ' || c
== '\t')
165 value
= parse_value();
169 return fn(name
, value
, data
);
172 static int get_extended_base_var(char *name
, int baselen
, int c
)
178 } while (isspace(c
));
180 /* We require the format to be '[base "extension"]' */
183 name
[baselen
++] = '.';
186 int ch
= get_next_char();
193 ch
= get_next_char();
197 name
[baselen
++] = ch
;
198 if (baselen
> MAXNAME
/ 2)
203 if (get_next_char() != ']')
208 static int get_base_var(char *name
)
213 int c
= get_next_char();
219 return get_extended_base_var(name
, baselen
, c
);
220 if (!iskeychar(c
) && c
!= '.')
222 if (baselen
> MAXNAME
/ 2)
224 name
[baselen
++] = tolower(c
);
228 static int perf_parse_file(config_fn_t fn
, void *data
)
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
;
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
) {
249 /* Do not tolerate partial BOM. */
250 if (bomptr
!= utf8_bom
)
252 /* No BOM at file beginning. Cool. */
262 if (comment
|| isspace(c
))
264 if (c
== '#' || c
== ';') {
269 baselen
= get_base_var(var
);
272 var
[baselen
++] = '.';
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
;
290 pr_err("bad config file line %d in %s\n", config_linenr
, config_file_name
);
294 static int parse_unit_factor(const char *end
, unsigned long *val
)
298 else if (!strcasecmp(end
, "k")) {
302 else if (!strcasecmp(end
, "m")) {
306 else if (!strcasecmp(end
, "g")) {
307 *val
*= 1024 * 1024 * 1024;
313 static int perf_parse_llong(const char *value
, long long *ret
)
315 if (value
&& *value
) {
317 long long val
= strtoll(value
, &end
, 0);
318 unsigned long factor
= 1;
320 if (!parse_unit_factor(end
, &factor
))
328 static int perf_parse_long(const char *value
, long *ret
)
330 if (value
&& *value
) {
332 long val
= strtol(value
, &end
, 0);
333 unsigned long factor
= 1;
334 if (!parse_unit_factor(end
, &factor
))
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
);
347 pr_warning("bad config value for '%s', ignoring...\n", name
);
350 int perf_config_u64(u64
*dest
, const char *name
, const char *value
)
354 if (!perf_parse_llong(value
, &ret
)) {
363 int perf_config_int(int *dest
, const char *name
, const char *value
)
366 if (!perf_parse_long(value
, &ret
)) {
374 static int perf_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
383 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
385 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
388 return perf_config_int(&ret
, name
, value
) < 0 ? -1 : ret
;
391 int perf_config_bool(const char *name
, const char *value
)
394 return !!perf_config_bool_or_int(name
, value
, &discard
);
397 static const char *perf_config_dirname(const char *name
, const char *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
);
411 pr_err("Invalid buildid directory!\n");
414 strncpy(buildid_dir
, dir
, MAXPATHLEN
-1);
415 buildid_dir
[MAXPATHLEN
-1] = '\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. */
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
);
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. */
465 static int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
468 FILE *f
= fopen(filename
, "r");
473 config_file_name
= filename
;
476 ret
= perf_parse_file(fn
, data
);
478 config_file_name
= NULL
;
483 const char *perf_etc_perfconfig(void)
485 static const char *system_wide
;
487 system_wide
= system_path(ETC_PERFCONFIG
);
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
))
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
, §ion
->items
, node
)
525 if (!strcmp(item
->name
, name
))
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
));
539 INIT_LIST_HEAD(§ion
->items
);
540 section
->name
= strdup(section_name
);
541 if (!section
->name
) {
542 pr_debug("%s: strdup failed\n", __func__
);
547 list_add_tail(§ion
->node
, sections
);
551 static struct perf_config_item
*add_config_item(struct perf_config_section
*section
,
554 struct perf_config_item
*item
= zalloc(sizeof(*item
));
559 item
->name
= strdup(name
);
561 pr_debug("%s: strdup failed\n", __func__
);
566 list_add_tail(&item
->node
, §ion
->items
);
570 static int set_value(struct perf_config_item
*item
, const char *value
)
572 char *val
= strdup(value
);
582 static int collect_config(const char *var
, const char *value
,
583 void *perf_config_set
)
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
;
596 sections
= &set
->sections
;
597 key
= ptr
= strdup(var
);
599 pr_debug("%s: strdup failed\n", __func__
);
603 section_name
= strsep(&ptr
, ".");
605 if (name
== NULL
|| value
== NULL
)
608 section
= find_section(sections
, section_name
);
610 section
= add_section(sections
, section_name
);
615 item
= find_config_item(name
, section
);
617 item
= add_config_item(section
, name
);
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;
631 section
->from_system_config
= false;
632 item
->from_system_config
= false;
635 ret
= set_value(item
, value
);
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
)
652 const char *home
= NULL
;
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)
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())
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
);
680 if (stat(user_config
, &st
) < 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
);
694 ret
= perf_config_from_file(collect_config
, user_config
, set
);
702 struct perf_config_set
*perf_config_set__new(void)
704 struct perf_config_set
*set
= zalloc(sizeof(*set
));
707 INIT_LIST_HEAD(&set
->sections
);
708 perf_config_set__init(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
)
726 struct perf_config_section
*section
;
727 struct perf_config_item
*item
;
729 if (config_set
== NULL
&& perf_config__init())
732 perf_config_set__for_each_entry(config_set
, section
, item
) {
733 char *value
= item
->value
;
736 scnprintf(key
, sizeof(key
), "%s.%s",
737 section
->name
, item
->name
);
738 ret
= fn(key
, value
, data
);
740 pr_err("Error: wrong config key-value pair %s=%s\n",
750 void perf_config__exit(void)
752 perf_config_set__delete(config_set
);
756 void perf_config__refresh(void)
762 static void perf_config_item__delete(struct perf_config_item
*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
, §ion
->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(§ion
->name
);
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(§ion
->node
);
792 perf_config_section__delete(section
);
796 void perf_config_set__delete(struct perf_config_set
*set
)
801 perf_config_set__purge(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
);
815 void set_buildid_dir(const char *dir
)
818 scnprintf(buildid_dir
, MAXPATHLEN
, "%s", dir
);
820 /* default to $HOME/.debug */
821 if (buildid_dir
[0] == '\0') {
822 char *home
= getenv("HOME");
825 snprintf(buildid_dir
, MAXPATHLEN
, "%s/%s",
826 home
, DEBUG_CACHE_DIR
);
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);