0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <errno.h>
0013 #include <sys/param.h>
0014 #include "cache.h"
0015 #include "callchain.h"
0016 #include <subcmd/exec-cmd.h>
0017 #include "util/event.h" /* proc_map_timeout */
0018 #include "util/hist.h" /* perf_hist_config */
0019 #include "util/llvm-utils.h" /* perf_llvm_config */
0020 #include "util/stat.h" /* perf_stat__set_big_num */
0021 #include "util/evsel.h" /* evsel__hw_names, evsel__use_bpf_counters */
0022 #include "build-id.h"
0023 #include "debug.h"
0024 #include "config.h"
0025 #include <sys/types.h>
0026 #include <sys/stat.h>
0027 #include <stdlib.h>
0028 #include <unistd.h>
0029 #include <linux/string.h>
0030 #include <linux/zalloc.h>
0031 #include <linux/ctype.h>
0032
0033 #define MAXNAME (256)
0034
0035 #define DEBUG_CACHE_DIR ".debug"
0036
0037
0038 char buildid_dir[MAXPATHLEN];
0039
0040 static FILE *config_file;
0041 static const char *config_file_name;
0042 static int config_linenr;
0043 static int config_file_eof;
0044 static struct perf_config_set *config_set;
0045
0046 const char *config_exclusive_filename;
0047
0048 static int get_next_char(void)
0049 {
0050 int c;
0051 FILE *f;
0052
0053 c = '\n';
0054 if ((f = config_file) != NULL) {
0055 c = fgetc(f);
0056 if (c == '\r') {
0057
0058 c = fgetc(f);
0059 if (c != '\n') {
0060 ungetc(c, f);
0061 c = '\r';
0062 }
0063 }
0064 if (c == '\n')
0065 config_linenr++;
0066 if (c == EOF) {
0067 config_file_eof = 1;
0068 c = '\n';
0069 }
0070 }
0071 return c;
0072 }
0073
0074 static char *parse_value(void)
0075 {
0076 static char value[1024];
0077 int quote = 0, comment = 0, space = 0;
0078 size_t len = 0;
0079
0080 for (;;) {
0081 int c = get_next_char();
0082
0083 if (len >= sizeof(value) - 1)
0084 return NULL;
0085 if (c == '\n') {
0086 if (quote)
0087 return NULL;
0088 value[len] = 0;
0089 return value;
0090 }
0091 if (comment)
0092 continue;
0093 if (isspace(c) && !quote) {
0094 space = 1;
0095 continue;
0096 }
0097 if (!quote) {
0098 if (c == ';' || c == '#') {
0099 comment = 1;
0100 continue;
0101 }
0102 }
0103 if (space) {
0104 if (len)
0105 value[len++] = ' ';
0106 space = 0;
0107 }
0108 if (c == '\\') {
0109 c = get_next_char();
0110 switch (c) {
0111 case '\n':
0112 continue;
0113 case 't':
0114 c = '\t';
0115 break;
0116 case 'b':
0117 c = '\b';
0118 break;
0119 case 'n':
0120 c = '\n';
0121 break;
0122
0123 case '\\': case '"':
0124 break;
0125
0126 default:
0127 return NULL;
0128 }
0129 value[len++] = c;
0130 continue;
0131 }
0132 if (c == '"') {
0133 quote = 1-quote;
0134 continue;
0135 }
0136 value[len++] = c;
0137 }
0138 }
0139
0140 static inline int iskeychar(int c)
0141 {
0142 return isalnum(c) || c == '-' || c == '_';
0143 }
0144
0145 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
0146 {
0147 int c;
0148 char *value;
0149
0150
0151 for (;;) {
0152 c = get_next_char();
0153 if (config_file_eof)
0154 break;
0155 if (!iskeychar(c))
0156 break;
0157 name[len++] = c;
0158 if (len >= MAXNAME)
0159 return -1;
0160 }
0161 name[len] = 0;
0162 while (c == ' ' || c == '\t')
0163 c = get_next_char();
0164
0165 value = NULL;
0166 if (c != '\n') {
0167 if (c != '=')
0168 return -1;
0169 value = parse_value();
0170 if (!value)
0171 return -1;
0172 }
0173 return fn(name, value, data);
0174 }
0175
0176 static int get_extended_base_var(char *name, int baselen, int c)
0177 {
0178 do {
0179 if (c == '\n')
0180 return -1;
0181 c = get_next_char();
0182 } while (isspace(c));
0183
0184
0185 if (c != '"')
0186 return -1;
0187 name[baselen++] = '.';
0188
0189 for (;;) {
0190 int ch = get_next_char();
0191
0192 if (ch == '\n')
0193 return -1;
0194 if (ch == '"')
0195 break;
0196 if (ch == '\\') {
0197 ch = get_next_char();
0198 if (ch == '\n')
0199 return -1;
0200 }
0201 name[baselen++] = ch;
0202 if (baselen > MAXNAME / 2)
0203 return -1;
0204 }
0205
0206
0207 if (get_next_char() != ']')
0208 return -1;
0209 return baselen;
0210 }
0211
0212 static int get_base_var(char *name)
0213 {
0214 int baselen = 0;
0215
0216 for (;;) {
0217 int c = get_next_char();
0218 if (config_file_eof)
0219 return -1;
0220 if (c == ']')
0221 return baselen;
0222 if (isspace(c))
0223 return get_extended_base_var(name, baselen, c);
0224 if (!iskeychar(c) && c != '.')
0225 return -1;
0226 if (baselen > MAXNAME / 2)
0227 return -1;
0228 name[baselen++] = tolower(c);
0229 }
0230 }
0231
0232 static int perf_parse_file(config_fn_t fn, void *data)
0233 {
0234 int comment = 0;
0235 int baselen = 0;
0236 static char var[MAXNAME];
0237
0238
0239 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
0240 const unsigned char *bomptr = utf8_bom;
0241
0242 for (;;) {
0243 int line, c = get_next_char();
0244
0245 if (bomptr && *bomptr) {
0246
0247
0248
0249 if ((unsigned char) c == *bomptr) {
0250 bomptr++;
0251 continue;
0252 } else {
0253
0254 if (bomptr != utf8_bom)
0255 break;
0256
0257 bomptr = NULL;
0258 }
0259 }
0260 if (c == '\n') {
0261 if (config_file_eof)
0262 return 0;
0263 comment = 0;
0264 continue;
0265 }
0266 if (comment || isspace(c))
0267 continue;
0268 if (c == '#' || c == ';') {
0269 comment = 1;
0270 continue;
0271 }
0272 if (c == '[') {
0273 baselen = get_base_var(var);
0274 if (baselen <= 0)
0275 break;
0276 var[baselen++] = '.';
0277 var[baselen] = 0;
0278 continue;
0279 }
0280 if (!isalpha(c))
0281 break;
0282 var[baselen] = tolower(c);
0283
0284
0285
0286
0287
0288 line = config_linenr;
0289 if (get_value(fn, data, var, baselen+1) < 0) {
0290 config_linenr = line;
0291 break;
0292 }
0293 }
0294 pr_err("bad config file line %d in %s\n", config_linenr, config_file_name);
0295 return -1;
0296 }
0297
0298 static int parse_unit_factor(const char *end, unsigned long *val)
0299 {
0300 if (!*end)
0301 return 1;
0302 else if (!strcasecmp(end, "k")) {
0303 *val *= 1024;
0304 return 1;
0305 }
0306 else if (!strcasecmp(end, "m")) {
0307 *val *= 1024 * 1024;
0308 return 1;
0309 }
0310 else if (!strcasecmp(end, "g")) {
0311 *val *= 1024 * 1024 * 1024;
0312 return 1;
0313 }
0314 return 0;
0315 }
0316
0317 static int perf_parse_llong(const char *value, long long *ret)
0318 {
0319 if (value && *value) {
0320 char *end;
0321 long long val = strtoll(value, &end, 0);
0322 unsigned long factor = 1;
0323
0324 if (!parse_unit_factor(end, &factor))
0325 return 0;
0326 *ret = val * factor;
0327 return 1;
0328 }
0329 return 0;
0330 }
0331
0332 static int perf_parse_long(const char *value, long *ret)
0333 {
0334 if (value && *value) {
0335 char *end;
0336 long val = strtol(value, &end, 0);
0337 unsigned long factor = 1;
0338 if (!parse_unit_factor(end, &factor))
0339 return 0;
0340 *ret = val * factor;
0341 return 1;
0342 }
0343 return 0;
0344 }
0345
0346 static void bad_config(const char *name)
0347 {
0348 if (config_file_name)
0349 pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name);
0350 else
0351 pr_warning("bad config value for '%s', ignoring...\n", name);
0352 }
0353
0354 int perf_config_u64(u64 *dest, const char *name, const char *value)
0355 {
0356 long long ret = 0;
0357
0358 if (!perf_parse_llong(value, &ret)) {
0359 bad_config(name);
0360 return -1;
0361 }
0362
0363 *dest = ret;
0364 return 0;
0365 }
0366
0367 int perf_config_int(int *dest, const char *name, const char *value)
0368 {
0369 long ret = 0;
0370 if (!perf_parse_long(value, &ret)) {
0371 bad_config(name);
0372 return -1;
0373 }
0374 *dest = ret;
0375 return 0;
0376 }
0377
0378 int perf_config_u8(u8 *dest, const char *name, const char *value)
0379 {
0380 long ret = 0;
0381
0382 if (!perf_parse_long(value, &ret)) {
0383 bad_config(name);
0384 return -1;
0385 }
0386 *dest = ret;
0387 return 0;
0388 }
0389
0390 static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
0391 {
0392 int ret;
0393
0394 *is_bool = 1;
0395 if (!value)
0396 return 1;
0397 if (!*value)
0398 return 0;
0399 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
0400 return 1;
0401 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
0402 return 0;
0403 *is_bool = 0;
0404 return perf_config_int(&ret, name, value) < 0 ? -1 : ret;
0405 }
0406
0407 int perf_config_bool(const char *name, const char *value)
0408 {
0409 int discard;
0410 return !!perf_config_bool_or_int(name, value, &discard);
0411 }
0412
0413 static const char *perf_config_dirname(const char *name, const char *value)
0414 {
0415 if (!name)
0416 return NULL;
0417 return value;
0418 }
0419
0420 static int perf_buildid_config(const char *var, const char *value)
0421 {
0422
0423 if (!strcmp(var, "buildid.dir")) {
0424 const char *dir = perf_config_dirname(var, value);
0425
0426 if (!dir) {
0427 pr_err("Invalid buildid directory!\n");
0428 return -1;
0429 }
0430 strncpy(buildid_dir, dir, MAXPATHLEN-1);
0431 buildid_dir[MAXPATHLEN-1] = '\0';
0432 }
0433
0434 return 0;
0435 }
0436
0437 static int perf_default_core_config(const char *var __maybe_unused,
0438 const char *value __maybe_unused)
0439 {
0440 if (!strcmp(var, "core.proc-map-timeout"))
0441 proc_map_timeout = strtoul(value, NULL, 10);
0442
0443
0444 return 0;
0445 }
0446
0447 static int perf_ui_config(const char *var, const char *value)
0448 {
0449
0450 if (!strcmp(var, "ui.show-headers"))
0451 symbol_conf.show_hist_headers = perf_config_bool(var, value);
0452
0453 return 0;
0454 }
0455
0456 static int perf_stat_config(const char *var, const char *value)
0457 {
0458 if (!strcmp(var, "stat.big-num"))
0459 perf_stat__set_big_num(perf_config_bool(var, value));
0460
0461 if (!strcmp(var, "stat.no-csv-summary"))
0462 perf_stat__set_no_csv_summary(perf_config_bool(var, value));
0463
0464 if (!strcmp(var, "stat.bpf-counter-events"))
0465 evsel__bpf_counter_events = strdup(value);
0466
0467
0468 return 0;
0469 }
0470
0471 int perf_default_config(const char *var, const char *value,
0472 void *dummy __maybe_unused)
0473 {
0474 if (strstarts(var, "core."))
0475 return perf_default_core_config(var, value);
0476
0477 if (strstarts(var, "hist."))
0478 return perf_hist_config(var, value);
0479
0480 if (strstarts(var, "ui."))
0481 return perf_ui_config(var, value);
0482
0483 if (strstarts(var, "call-graph."))
0484 return perf_callchain_config(var, value);
0485
0486 if (strstarts(var, "llvm."))
0487 return perf_llvm_config(var, value);
0488
0489 if (strstarts(var, "buildid."))
0490 return perf_buildid_config(var, value);
0491
0492 if (strstarts(var, "stat."))
0493 return perf_stat_config(var, value);
0494
0495
0496 return 0;
0497 }
0498
0499 static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
0500 {
0501 int ret;
0502 FILE *f = fopen(filename, "r");
0503
0504 ret = -1;
0505 if (f) {
0506 config_file = f;
0507 config_file_name = filename;
0508 config_linenr = 1;
0509 config_file_eof = 0;
0510 ret = perf_parse_file(fn, data);
0511 fclose(f);
0512 config_file_name = NULL;
0513 }
0514 return ret;
0515 }
0516
0517 const char *perf_etc_perfconfig(void)
0518 {
0519 static const char *system_wide;
0520 if (!system_wide)
0521 system_wide = system_path(ETC_PERFCONFIG);
0522 return system_wide;
0523 }
0524
0525 static int perf_env_bool(const char *k, int def)
0526 {
0527 const char *v = getenv(k);
0528 return v ? perf_config_bool(k, v) : def;
0529 }
0530
0531 int perf_config_system(void)
0532 {
0533 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
0534 }
0535
0536 int perf_config_global(void)
0537 {
0538 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
0539 }
0540
0541 static char *home_perfconfig(void)
0542 {
0543 const char *home = NULL;
0544 char *config;
0545 struct stat st;
0546
0547 home = getenv("HOME");
0548
0549
0550
0551
0552
0553
0554 if (!home || !*home || !perf_config_global())
0555 return NULL;
0556
0557 config = strdup(mkpath("%s/.perfconfig", home));
0558 if (config == NULL) {
0559 pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home);
0560 return NULL;
0561 }
0562
0563 if (stat(config, &st) < 0)
0564 goto out_free;
0565
0566 if (st.st_uid && (st.st_uid != geteuid())) {
0567 pr_warning("File %s not owned by current user or root, ignoring it.", config);
0568 goto out_free;
0569 }
0570
0571 if (st.st_size)
0572 return config;
0573
0574 out_free:
0575 free(config);
0576 return NULL;
0577 }
0578
0579 const char *perf_home_perfconfig(void)
0580 {
0581 static const char *config;
0582 static bool failed;
0583
0584 if (failed || config)
0585 return config;
0586
0587 config = home_perfconfig();
0588 if (!config)
0589 failed = true;
0590
0591 return config;
0592 }
0593
0594 static struct perf_config_section *find_section(struct list_head *sections,
0595 const char *section_name)
0596 {
0597 struct perf_config_section *section;
0598
0599 list_for_each_entry(section, sections, node)
0600 if (!strcmp(section->name, section_name))
0601 return section;
0602
0603 return NULL;
0604 }
0605
0606 static struct perf_config_item *find_config_item(const char *name,
0607 struct perf_config_section *section)
0608 {
0609 struct perf_config_item *item;
0610
0611 list_for_each_entry(item, §ion->items, node)
0612 if (!strcmp(item->name, name))
0613 return item;
0614
0615 return NULL;
0616 }
0617
0618 static struct perf_config_section *add_section(struct list_head *sections,
0619 const char *section_name)
0620 {
0621 struct perf_config_section *section = zalloc(sizeof(*section));
0622
0623 if (!section)
0624 return NULL;
0625
0626 INIT_LIST_HEAD(§ion->items);
0627 section->name = strdup(section_name);
0628 if (!section->name) {
0629 pr_debug("%s: strdup failed\n", __func__);
0630 free(section);
0631 return NULL;
0632 }
0633
0634 list_add_tail(§ion->node, sections);
0635 return section;
0636 }
0637
0638 static struct perf_config_item *add_config_item(struct perf_config_section *section,
0639 const char *name)
0640 {
0641 struct perf_config_item *item = zalloc(sizeof(*item));
0642
0643 if (!item)
0644 return NULL;
0645
0646 item->name = strdup(name);
0647 if (!item->name) {
0648 pr_debug("%s: strdup failed\n", __func__);
0649 free(item);
0650 return NULL;
0651 }
0652
0653 list_add_tail(&item->node, §ion->items);
0654 return item;
0655 }
0656
0657 static int set_value(struct perf_config_item *item, const char *value)
0658 {
0659 char *val = strdup(value);
0660
0661 if (!val)
0662 return -1;
0663
0664 zfree(&item->value);
0665 item->value = val;
0666 return 0;
0667 }
0668
0669 static int collect_config(const char *var, const char *value,
0670 void *perf_config_set)
0671 {
0672 int ret = -1;
0673 char *ptr, *key;
0674 char *section_name, *name;
0675 struct perf_config_section *section = NULL;
0676 struct perf_config_item *item = NULL;
0677 struct perf_config_set *set = perf_config_set;
0678 struct list_head *sections;
0679
0680 if (set == NULL)
0681 return -1;
0682
0683 sections = &set->sections;
0684 key = ptr = strdup(var);
0685 if (!key) {
0686 pr_debug("%s: strdup failed\n", __func__);
0687 return -1;
0688 }
0689
0690 section_name = strsep(&ptr, ".");
0691 name = ptr;
0692 if (name == NULL || value == NULL)
0693 goto out_free;
0694
0695 section = find_section(sections, section_name);
0696 if (!section) {
0697 section = add_section(sections, section_name);
0698 if (!section)
0699 goto out_free;
0700 }
0701
0702 item = find_config_item(name, section);
0703 if (!item) {
0704 item = add_config_item(section, name);
0705 if (!item)
0706 goto out_free;
0707 }
0708
0709
0710
0711
0712
0713
0714 if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) {
0715 section->from_system_config = true;
0716 item->from_system_config = true;
0717 } else {
0718 section->from_system_config = false;
0719 item->from_system_config = false;
0720 }
0721
0722 ret = set_value(item, value);
0723
0724 out_free:
0725 free(key);
0726 return ret;
0727 }
0728
0729 int perf_config_set__collect(struct perf_config_set *set, const char *file_name,
0730 const char *var, const char *value)
0731 {
0732 config_file_name = file_name;
0733 return collect_config(var, value, set);
0734 }
0735
0736 static int perf_config_set__init(struct perf_config_set *set)
0737 {
0738 int ret = -1;
0739
0740
0741 if (config_exclusive_filename)
0742 return perf_config_from_file(collect_config, config_exclusive_filename, set);
0743 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
0744 if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0)
0745 goto out;
0746 }
0747 if (perf_config_global() && perf_home_perfconfig()) {
0748 if (perf_config_from_file(collect_config, perf_home_perfconfig(), set) < 0)
0749 goto out;
0750 }
0751
0752 out:
0753 return ret;
0754 }
0755
0756 struct perf_config_set *perf_config_set__new(void)
0757 {
0758 struct perf_config_set *set = zalloc(sizeof(*set));
0759
0760 if (set) {
0761 INIT_LIST_HEAD(&set->sections);
0762 perf_config_set__init(set);
0763 }
0764
0765 return set;
0766 }
0767
0768 struct perf_config_set *perf_config_set__load_file(const char *file)
0769 {
0770 struct perf_config_set *set = zalloc(sizeof(*set));
0771
0772 if (set) {
0773 INIT_LIST_HEAD(&set->sections);
0774 perf_config_from_file(collect_config, file, set);
0775 }
0776
0777 return set;
0778 }
0779
0780 static int perf_config__init(void)
0781 {
0782 if (config_set == NULL)
0783 config_set = perf_config_set__new();
0784
0785 return config_set == NULL;
0786 }
0787
0788 int perf_config_set(struct perf_config_set *set,
0789 config_fn_t fn, void *data)
0790 {
0791 int ret = 0;
0792 char key[BUFSIZ];
0793 struct perf_config_section *section;
0794 struct perf_config_item *item;
0795
0796 perf_config_set__for_each_entry(set, section, item) {
0797 char *value = item->value;
0798
0799 if (value) {
0800 scnprintf(key, sizeof(key), "%s.%s",
0801 section->name, item->name);
0802 ret = fn(key, value, data);
0803 if (ret < 0) {
0804 pr_err("Error in the given config file: wrong config key-value pair %s=%s\n",
0805 key, value);
0806
0807
0808
0809
0810 goto out;
0811 }
0812 }
0813 }
0814 out:
0815 return ret;
0816 }
0817
0818 int perf_config(config_fn_t fn, void *data)
0819 {
0820 if (config_set == NULL && perf_config__init())
0821 return -1;
0822
0823 return perf_config_set(config_set, fn, data);
0824 }
0825
0826 void perf_config__exit(void)
0827 {
0828 perf_config_set__delete(config_set);
0829 config_set = NULL;
0830 }
0831
0832 void perf_config__refresh(void)
0833 {
0834 perf_config__exit();
0835 perf_config__init();
0836 }
0837
0838 static void perf_config_item__delete(struct perf_config_item *item)
0839 {
0840 zfree(&item->name);
0841 zfree(&item->value);
0842 free(item);
0843 }
0844
0845 static void perf_config_section__purge(struct perf_config_section *section)
0846 {
0847 struct perf_config_item *item, *tmp;
0848
0849 list_for_each_entry_safe(item, tmp, §ion->items, node) {
0850 list_del_init(&item->node);
0851 perf_config_item__delete(item);
0852 }
0853 }
0854
0855 static void perf_config_section__delete(struct perf_config_section *section)
0856 {
0857 perf_config_section__purge(section);
0858 zfree(§ion->name);
0859 free(section);
0860 }
0861
0862 static void perf_config_set__purge(struct perf_config_set *set)
0863 {
0864 struct perf_config_section *section, *tmp;
0865
0866 list_for_each_entry_safe(section, tmp, &set->sections, node) {
0867 list_del_init(§ion->node);
0868 perf_config_section__delete(section);
0869 }
0870 }
0871
0872 void perf_config_set__delete(struct perf_config_set *set)
0873 {
0874 if (set == NULL)
0875 return;
0876
0877 perf_config_set__purge(set);
0878 free(set);
0879 }
0880
0881
0882
0883
0884
0885 int config_error_nonbool(const char *var)
0886 {
0887 pr_err("Missing value for '%s'", var);
0888 return -1;
0889 }
0890
0891 void set_buildid_dir(const char *dir)
0892 {
0893 if (dir)
0894 scnprintf(buildid_dir, MAXPATHLEN, "%s", dir);
0895
0896
0897 if (buildid_dir[0] == '\0') {
0898 char *home = getenv("HOME");
0899
0900 if (home) {
0901 snprintf(buildid_dir, MAXPATHLEN, "%s/%s",
0902 home, DEBUG_CACHE_DIR);
0903 } else {
0904 strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
0905 }
0906 buildid_dir[MAXPATHLEN-1] = '\0';
0907 }
0908
0909 setenv("PERF_BUILDID_DIR", buildid_dir, 1);
0910 }