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 pr_err("bad config file line %d in %s\n", config_linenr
, config_file_name
);
282 static int parse_unit_factor(const char *end
, unsigned long *val
)
286 else if (!strcasecmp(end
, "k")) {
290 else if (!strcasecmp(end
, "m")) {
294 else if (!strcasecmp(end
, "g")) {
295 *val
*= 1024 * 1024 * 1024;
301 static int perf_parse_llong(const char *value
, long long *ret
)
303 if (value
&& *value
) {
305 long long val
= strtoll(value
, &end
, 0);
306 unsigned long factor
= 1;
308 if (!parse_unit_factor(end
, &factor
))
316 static int perf_parse_long(const char *value
, long *ret
)
318 if (value
&& *value
) {
320 long val
= strtol(value
, &end
, 0);
321 unsigned long factor
= 1;
322 if (!parse_unit_factor(end
, &factor
))
330 static void die_bad_config(const char *name
)
332 if (config_file_name
)
333 die("bad config value for '%s' in %s", name
, config_file_name
);
334 die("bad config value for '%s'", name
);
337 u64
perf_config_u64(const char *name
, const char *value
)
341 if (!perf_parse_llong(value
, &ret
))
342 die_bad_config(name
);
346 int perf_config_int(const char *name
, const char *value
)
349 if (!perf_parse_long(value
, &ret
))
350 die_bad_config(name
);
354 static int perf_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
361 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
363 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
366 return perf_config_int(name
, value
);
369 int perf_config_bool(const char *name
, const char *value
)
372 return !!perf_config_bool_or_int(name
, value
, &discard
);
375 const char *perf_config_dirname(const char *name
, const char *value
)
382 static int perf_buildid_config(const char *var
, const char *value
)
384 /* same dir for all commands */
385 if (!strcmp(var
, "buildid.dir")) {
386 const char *dir
= perf_config_dirname(var
, value
);
390 strncpy(buildid_dir
, dir
, MAXPATHLEN
-1);
391 buildid_dir
[MAXPATHLEN
-1] = '\0';
397 static int perf_default_core_config(const char *var __maybe_unused
,
398 const char *value __maybe_unused
)
400 /* Add other config variables here. */
404 static int perf_ui_config(const char *var
, const char *value
)
406 /* Add other config variables here. */
407 if (!strcmp(var
, "ui.show-headers")) {
408 symbol_conf
.show_hist_headers
= perf_config_bool(var
, value
);
414 int perf_default_config(const char *var
, const char *value
,
415 void *dummy __maybe_unused
)
417 if (!prefixcmp(var
, "core."))
418 return perf_default_core_config(var
, value
);
420 if (!prefixcmp(var
, "hist."))
421 return perf_hist_config(var
, value
);
423 if (!prefixcmp(var
, "ui."))
424 return perf_ui_config(var
, value
);
426 if (!prefixcmp(var
, "call-graph."))
427 return perf_callchain_config(var
, value
);
429 if (!prefixcmp(var
, "llvm."))
430 return perf_llvm_config(var
, value
);
432 if (!prefixcmp(var
, "buildid."))
433 return perf_buildid_config(var
, value
);
435 /* Add other config variables here. */
439 static int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
442 FILE *f
= fopen(filename
, "r");
447 config_file_name
= filename
;
450 ret
= perf_parse_file(fn
, data
);
452 config_file_name
= NULL
;
457 const char *perf_etc_perfconfig(void)
459 static const char *system_wide
;
461 system_wide
= system_path(ETC_PERFCONFIG
);
465 static int perf_env_bool(const char *k
, int def
)
467 const char *v
= getenv(k
);
468 return v
? perf_config_bool(k
, v
) : def
;
471 static int perf_config_system(void)
473 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
476 static int perf_config_global(void)
478 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
481 int perf_config(config_fn_t fn
, void *data
)
484 const char *home
= NULL
;
486 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
487 if (config_exclusive_filename
)
488 return perf_config_from_file(fn
, config_exclusive_filename
, data
);
489 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
)) {
490 if (perf_config_from_file(fn
, perf_etc_perfconfig(), data
) < 0)
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
);
526 static struct perf_config_section
*find_section(struct list_head
*sections
,
527 const char *section_name
)
529 struct perf_config_section
*section
;
531 list_for_each_entry(section
, sections
, node
)
532 if (!strcmp(section
->name
, section_name
))
538 static struct perf_config_item
*find_config_item(const char *name
,
539 struct perf_config_section
*section
)
541 struct perf_config_item
*item
;
543 list_for_each_entry(item
, §ion
->items
, node
)
544 if (!strcmp(item
->name
, name
))
550 static struct perf_config_section
*add_section(struct list_head
*sections
,
551 const char *section_name
)
553 struct perf_config_section
*section
= zalloc(sizeof(*section
));
558 INIT_LIST_HEAD(§ion
->items
);
559 section
->name
= strdup(section_name
);
560 if (!section
->name
) {
561 pr_debug("%s: strdup failed\n", __func__
);
566 list_add_tail(§ion
->node
, sections
);
570 static struct perf_config_item
*add_config_item(struct perf_config_section
*section
,
573 struct perf_config_item
*item
= zalloc(sizeof(*item
));
578 item
->name
= strdup(name
);
580 pr_debug("%s: strdup failed\n", __func__
);
585 list_add_tail(&item
->node
, §ion
->items
);
589 static int set_value(struct perf_config_item
*item
, const char *value
)
591 char *val
= strdup(value
);
601 static int collect_config(const char *var
, const char *value
,
602 void *perf_config_set
)
606 char *section_name
, *name
;
607 struct perf_config_section
*section
= NULL
;
608 struct perf_config_item
*item
= NULL
;
609 struct perf_config_set
*set
= perf_config_set
;
610 struct list_head
*sections
;
615 sections
= &set
->sections
;
616 key
= ptr
= strdup(var
);
618 pr_debug("%s: strdup failed\n", __func__
);
622 section_name
= strsep(&ptr
, ".");
624 if (name
== NULL
|| value
== NULL
)
627 section
= find_section(sections
, section_name
);
629 section
= add_section(sections
, section_name
);
634 item
= find_config_item(name
, section
);
636 item
= add_config_item(section
, name
);
641 ret
= set_value(item
, value
);
649 struct perf_config_set
*perf_config_set__new(void)
651 struct perf_config_set
*set
= zalloc(sizeof(*set
));
654 INIT_LIST_HEAD(&set
->sections
);
655 if (perf_config(collect_config
, set
) < 0) {
656 perf_config_set__delete(set
);
664 static void perf_config_item__delete(struct perf_config_item
*item
)
671 static void perf_config_section__purge(struct perf_config_section
*section
)
673 struct perf_config_item
*item
, *tmp
;
675 list_for_each_entry_safe(item
, tmp
, §ion
->items
, node
) {
676 list_del_init(&item
->node
);
677 perf_config_item__delete(item
);
681 static void perf_config_section__delete(struct perf_config_section
*section
)
683 perf_config_section__purge(section
);
684 zfree(§ion
->name
);
688 static void perf_config_set__purge(struct perf_config_set
*set
)
690 struct perf_config_section
*section
, *tmp
;
692 list_for_each_entry_safe(section
, tmp
, &set
->sections
, node
) {
693 list_del_init(§ion
->node
);
694 perf_config_section__delete(section
);
698 void perf_config_set__delete(struct perf_config_set
*set
)
700 perf_config_set__purge(set
);
705 * Call this to report error for your variable that should not
706 * get a boolean value (i.e. "[my] var" means "true").
708 int config_error_nonbool(const char *var
)
710 return error("Missing value for '%s'", var
);
713 void set_buildid_dir(const char *dir
)
716 scnprintf(buildid_dir
, MAXPATHLEN
-1, "%s", dir
);
718 /* default to $HOME/.debug */
719 if (buildid_dir
[0] == '\0') {
720 char *home
= getenv("HOME");
723 snprintf(buildid_dir
, MAXPATHLEN
-1, "%s/%s",
724 home
, DEBUG_CACHE_DIR
);
726 strncpy(buildid_dir
, DEBUG_CACHE_DIR
, MAXPATHLEN
-1);
728 buildid_dir
[MAXPATHLEN
-1] = '\0';
730 /* for communicating with external commands */
731 setenv("PERF_BUILDID_DIR", buildid_dir
, 1);