config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * config.c
  4. *
  5. * Helper functions for parsing config items.
  6. * Originally copied from GIT source.
  7. *
  8. * Copyright (C) Linus Torvalds, 2005
  9. * Copyright (C) Johannes Schindelin, 2005
  10. *
  11. */
  12. #include <errno.h>
  13. #include <sys/param.h>
  14. #include "util.h"
  15. #include "cache.h"
  16. #include <subcmd/exec-cmd.h>
  17. #include "util/hist.h" /* perf_hist_config */
  18. #include "util/llvm-utils.h" /* perf_llvm_config */
  19. #include "config.h"
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <linux/string.h>
  24. #include "sane_ctype.h"
  25. #define MAXNAME (256)
  26. #define DEBUG_CACHE_DIR ".debug"
  27. char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
  28. static FILE *config_file;
  29. static const char *config_file_name;
  30. static int config_linenr;
  31. static int config_file_eof;
  32. static struct perf_config_set *config_set;
  33. const char *config_exclusive_filename;
  34. static int get_next_char(void)
  35. {
  36. int c;
  37. FILE *f;
  38. c = '\n';
  39. if ((f = config_file) != NULL) {
  40. c = fgetc(f);
  41. if (c == '\r') {
  42. /* DOS like systems */
  43. c = fgetc(f);
  44. if (c != '\n') {
  45. ungetc(c, f);
  46. c = '\r';
  47. }
  48. }
  49. if (c == '\n')
  50. config_linenr++;
  51. if (c == EOF) {
  52. config_file_eof = 1;
  53. c = '\n';
  54. }
  55. }
  56. return c;
  57. }
  58. static char *parse_value(void)
  59. {
  60. static char value[1024];
  61. int quote = 0, comment = 0, space = 0;
  62. size_t len = 0;
  63. for (;;) {
  64. int c = get_next_char();
  65. if (len >= sizeof(value) - 1)
  66. return NULL;
  67. if (c == '\n') {
  68. if (quote)
  69. return NULL;
  70. value[len] = 0;
  71. return value;
  72. }
  73. if (comment)
  74. continue;
  75. if (isspace(c) && !quote) {
  76. space = 1;
  77. continue;
  78. }
  79. if (!quote) {
  80. if (c == ';' || c == '#') {
  81. comment = 1;
  82. continue;
  83. }
  84. }
  85. if (space) {
  86. if (len)
  87. value[len++] = ' ';
  88. space = 0;
  89. }
  90. if (c == '\\') {
  91. c = get_next_char();
  92. switch (c) {
  93. case '\n':
  94. continue;
  95. case 't':
  96. c = '\t';
  97. break;
  98. case 'b':
  99. c = '\b';
  100. break;
  101. case 'n':
  102. c = '\n';
  103. break;
  104. /* Some characters escape as themselves */
  105. case '\\': case '"':
  106. break;
  107. /* Reject unknown escape sequences */
  108. default:
  109. return NULL;
  110. }
  111. value[len++] = c;
  112. continue;
  113. }
  114. if (c == '"') {
  115. quote = 1-quote;
  116. continue;
  117. }
  118. value[len++] = c;
  119. }
  120. }
  121. static inline int iskeychar(int c)
  122. {
  123. return isalnum(c) || c == '-' || c == '_';
  124. }
  125. static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
  126. {
  127. int c;
  128. char *value;
  129. /* Get the full name */
  130. for (;;) {
  131. c = get_next_char();
  132. if (config_file_eof)
  133. break;
  134. if (!iskeychar(c))
  135. break;
  136. name[len++] = c;
  137. if (len >= MAXNAME)
  138. return -1;
  139. }
  140. name[len] = 0;
  141. while (c == ' ' || c == '\t')
  142. c = get_next_char();
  143. value = NULL;
  144. if (c != '\n') {
  145. if (c != '=')
  146. return -1;
  147. value = parse_value();
  148. if (!value)
  149. return -1;
  150. }
  151. return fn(name, value, data);
  152. }
  153. static int get_extended_base_var(char *name, int baselen, int c)
  154. {
  155. do {
  156. if (c == '\n')
  157. return -1;
  158. c = get_next_char();
  159. } while (isspace(c));
  160. /* We require the format to be '[base "extension"]' */
  161. if (c != '"')
  162. return -1;
  163. name[baselen++] = '.';
  164. for (;;) {
  165. int ch = get_next_char();
  166. if (ch == '\n')
  167. return -1;
  168. if (ch == '"')
  169. break;
  170. if (ch == '\\') {
  171. ch = get_next_char();
  172. if (ch == '\n')
  173. return -1;
  174. }
  175. name[baselen++] = ch;
  176. if (baselen > MAXNAME / 2)
  177. return -1;
  178. }
  179. /* Final ']' */
  180. if (get_next_char() != ']')
  181. return -1;
  182. return baselen;
  183. }
  184. static int get_base_var(char *name)
  185. {
  186. int baselen = 0;
  187. for (;;) {
  188. int c = get_next_char();
  189. if (config_file_eof)
  190. return -1;
  191. if (c == ']')
  192. return baselen;
  193. if (isspace(c))
  194. return get_extended_base_var(name, baselen, c);
  195. if (!iskeychar(c) && c != '.')
  196. return -1;
  197. if (baselen > MAXNAME / 2)
  198. return -1;
  199. name[baselen++] = tolower(c);
  200. }
  201. }
  202. static int perf_parse_file(config_fn_t fn, void *data)
  203. {
  204. int comment = 0;
  205. int baselen = 0;
  206. static char var[MAXNAME];
  207. /* U+FEFF Byte Order Mark in UTF8 */
  208. static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
  209. const unsigned char *bomptr = utf8_bom;
  210. for (;;) {
  211. int line, c = get_next_char();
  212. if (bomptr && *bomptr) {
  213. /* We are at the file beginning; skip UTF8-encoded BOM
  214. * if present. Sane editors won't put this in on their
  215. * own, but e.g. Windows Notepad will do it happily. */
  216. if ((unsigned char) c == *bomptr) {
  217. bomptr++;
  218. continue;
  219. } else {
  220. /* Do not tolerate partial BOM. */
  221. if (bomptr != utf8_bom)
  222. break;
  223. /* No BOM at file beginning. Cool. */
  224. bomptr = NULL;
  225. }
  226. }
  227. if (c == '\n') {
  228. if (config_file_eof)
  229. return 0;
  230. comment = 0;
  231. continue;
  232. }
  233. if (comment || isspace(c))
  234. continue;
  235. if (c == '#' || c == ';') {
  236. comment = 1;
  237. continue;
  238. }
  239. if (c == '[') {
  240. baselen = get_base_var(var);
  241. if (baselen <= 0)
  242. break;
  243. var[baselen++] = '.';
  244. var[baselen] = 0;
  245. continue;
  246. }
  247. if (!isalpha(c))
  248. break;
  249. var[baselen] = tolower(c);
  250. /*
  251. * The get_value function might or might not reach the '\n',
  252. * so saving the current line number for error reporting.
  253. */
  254. line = config_linenr;
  255. if (get_value(fn, data, var, baselen+1) < 0) {
  256. config_linenr = line;
  257. break;
  258. }
  259. }
  260. pr_err("bad config file line %d in %s\n", config_linenr, config_file_name);
  261. return -1;
  262. }
  263. static int parse_unit_factor(const char *end, unsigned long *val)
  264. {
  265. if (!*end)
  266. return 1;
  267. else if (!strcasecmp(end, "k")) {
  268. *val *= 1024;
  269. return 1;
  270. }
  271. else if (!strcasecmp(end, "m")) {
  272. *val *= 1024 * 1024;
  273. return 1;
  274. }
  275. else if (!strcasecmp(end, "g")) {
  276. *val *= 1024 * 1024 * 1024;
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. static int perf_parse_llong(const char *value, long long *ret)
  282. {
  283. if (value && *value) {
  284. char *end;
  285. long long val = strtoll(value, &end, 0);
  286. unsigned long factor = 1;
  287. if (!parse_unit_factor(end, &factor))
  288. return 0;
  289. *ret = val * factor;
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. static int perf_parse_long(const char *value, long *ret)
  295. {
  296. if (value && *value) {
  297. char *end;
  298. long val = strtol(value, &end, 0);
  299. unsigned long factor = 1;
  300. if (!parse_unit_factor(end, &factor))
  301. return 0;
  302. *ret = val * factor;
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. static void bad_config(const char *name)
  308. {
  309. if (config_file_name)
  310. pr_warning("bad config value for '%s' in %s, ignoring...\n", name, config_file_name);
  311. else
  312. pr_warning("bad config value for '%s', ignoring...\n", name);
  313. }
  314. int perf_config_u64(u64 *dest, const char *name, const char *value)
  315. {
  316. long long ret = 0;
  317. if (!perf_parse_llong(value, &ret)) {
  318. bad_config(name);
  319. return -1;
  320. }
  321. *dest = ret;
  322. return 0;
  323. }
  324. int perf_config_int(int *dest, const char *name, const char *value)
  325. {
  326. long ret = 0;
  327. if (!perf_parse_long(value, &ret)) {
  328. bad_config(name);
  329. return -1;
  330. }
  331. *dest = ret;
  332. return 0;
  333. }
  334. static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
  335. {
  336. int ret;
  337. *is_bool = 1;
  338. if (!value)
  339. return 1;
  340. if (!*value)
  341. return 0;
  342. if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
  343. return 1;
  344. if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
  345. return 0;
  346. *is_bool = 0;
  347. return perf_config_int(&ret, name, value) < 0 ? -1 : ret;
  348. }
  349. int perf_config_bool(const char *name, const char *value)
  350. {
  351. int discard;
  352. return !!perf_config_bool_or_int(name, value, &discard);
  353. }
  354. static const char *perf_config_dirname(const char *name, const char *value)
  355. {
  356. if (!name)
  357. return NULL;
  358. return value;
  359. }
  360. static int perf_buildid_config(const char *var, const char *value)
  361. {
  362. /* same dir for all commands */
  363. if (!strcmp(var, "buildid.dir")) {
  364. const char *dir = perf_config_dirname(var, value);
  365. if (!dir) {
  366. pr_err("Invalid buildid directory!\n");
  367. return -1;
  368. }
  369. strncpy(buildid_dir, dir, MAXPATHLEN-1);
  370. buildid_dir[MAXPATHLEN-1] = '\0';
  371. }
  372. return 0;
  373. }
  374. static int perf_default_core_config(const char *var __maybe_unused,
  375. const char *value __maybe_unused)
  376. {
  377. /* Add other config variables here. */
  378. return 0;
  379. }
  380. static int perf_ui_config(const char *var, const char *value)
  381. {
  382. /* Add other config variables here. */
  383. if (!strcmp(var, "ui.show-headers"))
  384. symbol_conf.show_hist_headers = perf_config_bool(var, value);
  385. return 0;
  386. }
  387. int perf_default_config(const char *var, const char *value,
  388. void *dummy __maybe_unused)
  389. {
  390. if (strstarts(var, "core."))
  391. return perf_default_core_config(var, value);
  392. if (strstarts(var, "hist."))
  393. return perf_hist_config(var, value);
  394. if (strstarts(var, "ui."))
  395. return perf_ui_config(var, value);
  396. if (strstarts(var, "call-graph."))
  397. return perf_callchain_config(var, value);
  398. if (strstarts(var, "llvm."))
  399. return perf_llvm_config(var, value);
  400. if (strstarts(var, "buildid."))
  401. return perf_buildid_config(var, value);
  402. /* Add other config variables here. */
  403. return 0;
  404. }
  405. static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
  406. {
  407. int ret;
  408. FILE *f = fopen(filename, "r");
  409. ret = -1;
  410. if (f) {
  411. config_file = f;
  412. config_file_name = filename;
  413. config_linenr = 1;
  414. config_file_eof = 0;
  415. ret = perf_parse_file(fn, data);
  416. fclose(f);
  417. config_file_name = NULL;
  418. }
  419. return ret;
  420. }
  421. const char *perf_etc_perfconfig(void)
  422. {
  423. static const char *system_wide;
  424. if (!system_wide)
  425. system_wide = system_path(ETC_PERFCONFIG);
  426. return system_wide;
  427. }
  428. static int perf_env_bool(const char *k, int def)
  429. {
  430. const char *v = getenv(k);
  431. return v ? perf_config_bool(k, v) : def;
  432. }
  433. static int perf_config_system(void)
  434. {
  435. return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
  436. }
  437. static int perf_config_global(void)
  438. {
  439. return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
  440. }
  441. static struct perf_config_section *find_section(struct list_head *sections,
  442. const char *section_name)
  443. {
  444. struct perf_config_section *section;
  445. list_for_each_entry(section, sections, node)
  446. if (!strcmp(section->name, section_name))
  447. return section;
  448. return NULL;
  449. }
  450. static struct perf_config_item *find_config_item(const char *name,
  451. struct perf_config_section *section)
  452. {
  453. struct perf_config_item *item;
  454. list_for_each_entry(item, &section->items, node)
  455. if (!strcmp(item->name, name))
  456. return item;
  457. return NULL;
  458. }
  459. static struct perf_config_section *add_section(struct list_head *sections,
  460. const char *section_name)
  461. {
  462. struct perf_config_section *section = zalloc(sizeof(*section));
  463. if (!section)
  464. return NULL;
  465. INIT_LIST_HEAD(&section->items);
  466. section->name = strdup(section_name);
  467. if (!section->name) {
  468. pr_debug("%s: strdup failed\n", __func__);
  469. free(section);
  470. return NULL;
  471. }
  472. list_add_tail(&section->node, sections);
  473. return section;
  474. }
  475. static struct perf_config_item *add_config_item(struct perf_config_section *section,
  476. const char *name)
  477. {
  478. struct perf_config_item *item = zalloc(sizeof(*item));
  479. if (!item)
  480. return NULL;
  481. item->name = strdup(name);
  482. if (!item->name) {
  483. pr_debug("%s: strdup failed\n", __func__);
  484. free(item);
  485. return NULL;
  486. }
  487. list_add_tail(&item->node, &section->items);
  488. return item;
  489. }
  490. static int set_value(struct perf_config_item *item, const char *value)
  491. {
  492. char *val = strdup(value);
  493. if (!val)
  494. return -1;
  495. zfree(&item->value);
  496. item->value = val;
  497. return 0;
  498. }
  499. static int collect_config(const char *var, const char *value,
  500. void *perf_config_set)
  501. {
  502. int ret = -1;
  503. char *ptr, *key;
  504. char *section_name, *name;
  505. struct perf_config_section *section = NULL;
  506. struct perf_config_item *item = NULL;
  507. struct perf_config_set *set = perf_config_set;
  508. struct list_head *sections;
  509. if (set == NULL)
  510. return -1;
  511. sections = &set->sections;
  512. key = ptr = strdup(var);
  513. if (!key) {
  514. pr_debug("%s: strdup failed\n", __func__);
  515. return -1;
  516. }
  517. section_name = strsep(&ptr, ".");
  518. name = ptr;
  519. if (name == NULL || value == NULL)
  520. goto out_free;
  521. section = find_section(sections, section_name);
  522. if (!section) {
  523. section = add_section(sections, section_name);
  524. if (!section)
  525. goto out_free;
  526. }
  527. item = find_config_item(name, section);
  528. if (!item) {
  529. item = add_config_item(section, name);
  530. if (!item)
  531. goto out_free;
  532. }
  533. /* perf_config_set can contain both user and system config items.
  534. * So we should know where each value is from.
  535. * The classification would be needed when a particular config file
  536. * is overwrited by setting feature i.e. set_config().
  537. */
  538. if (strcmp(config_file_name, perf_etc_perfconfig()) == 0) {
  539. section->from_system_config = true;
  540. item->from_system_config = true;
  541. } else {
  542. section->from_system_config = false;
  543. item->from_system_config = false;
  544. }
  545. ret = set_value(item, value);
  546. out_free:
  547. free(key);
  548. return ret;
  549. }
  550. int perf_config_set__collect(struct perf_config_set *set, const char *file_name,
  551. const char *var, const char *value)
  552. {
  553. config_file_name = file_name;
  554. return collect_config(var, value, set);
  555. }
  556. static int perf_config_set__init(struct perf_config_set *set)
  557. {
  558. int ret = -1;
  559. const char *home = NULL;
  560. char *user_config;
  561. struct stat st;
  562. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  563. if (config_exclusive_filename)
  564. return perf_config_from_file(collect_config, config_exclusive_filename, set);
  565. if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
  566. if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0)
  567. goto out;
  568. }
  569. home = getenv("HOME");
  570. /*
  571. * Skip reading user config if:
  572. * - there is no place to read it from (HOME)
  573. * - we are asked not to (PERF_CONFIG_NOGLOBAL=1)
  574. */
  575. if (!home || !*home || !perf_config_global())
  576. return 0;
  577. user_config = strdup(mkpath("%s/.perfconfig", home));
  578. if (user_config == NULL) {
  579. pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home);
  580. goto out;
  581. }
  582. if (stat(user_config, &st) < 0) {
  583. if (errno == ENOENT)
  584. ret = 0;
  585. goto out_free;
  586. }
  587. ret = 0;
  588. if (st.st_uid && (st.st_uid != geteuid())) {
  589. pr_warning("File %s not owned by current user or root, ignoring it.", user_config);
  590. goto out_free;
  591. }
  592. if (st.st_size)
  593. ret = perf_config_from_file(collect_config, user_config, set);
  594. out_free:
  595. free(user_config);
  596. out:
  597. return ret;
  598. }
  599. struct perf_config_set *perf_config_set__new(void)
  600. {
  601. struct perf_config_set *set = zalloc(sizeof(*set));
  602. if (set) {
  603. INIT_LIST_HEAD(&set->sections);
  604. perf_config_set__init(set);
  605. }
  606. return set;
  607. }
  608. static int perf_config__init(void)
  609. {
  610. if (config_set == NULL)
  611. config_set = perf_config_set__new();
  612. return config_set == NULL;
  613. }
  614. int perf_config(config_fn_t fn, void *data)
  615. {
  616. int ret = 0;
  617. char key[BUFSIZ];
  618. struct perf_config_section *section;
  619. struct perf_config_item *item;
  620. if (config_set == NULL && perf_config__init())
  621. return -1;
  622. perf_config_set__for_each_entry(config_set, section, item) {
  623. char *value = item->value;
  624. if (value) {
  625. scnprintf(key, sizeof(key), "%s.%s",
  626. section->name, item->name);
  627. ret = fn(key, value, data);
  628. if (ret < 0) {
  629. pr_err("Error: wrong config key-value pair %s=%s\n",
  630. key, value);
  631. break;
  632. }
  633. }
  634. }
  635. return ret;
  636. }
  637. void perf_config__exit(void)
  638. {
  639. perf_config_set__delete(config_set);
  640. config_set = NULL;
  641. }
  642. void perf_config__refresh(void)
  643. {
  644. perf_config__exit();
  645. perf_config__init();
  646. }
  647. static void perf_config_item__delete(struct perf_config_item *item)
  648. {
  649. zfree(&item->name);
  650. zfree(&item->value);
  651. free(item);
  652. }
  653. static void perf_config_section__purge(struct perf_config_section *section)
  654. {
  655. struct perf_config_item *item, *tmp;
  656. list_for_each_entry_safe(item, tmp, &section->items, node) {
  657. list_del_init(&item->node);
  658. perf_config_item__delete(item);
  659. }
  660. }
  661. static void perf_config_section__delete(struct perf_config_section *section)
  662. {
  663. perf_config_section__purge(section);
  664. zfree(&section->name);
  665. free(section);
  666. }
  667. static void perf_config_set__purge(struct perf_config_set *set)
  668. {
  669. struct perf_config_section *section, *tmp;
  670. list_for_each_entry_safe(section, tmp, &set->sections, node) {
  671. list_del_init(&section->node);
  672. perf_config_section__delete(section);
  673. }
  674. }
  675. void perf_config_set__delete(struct perf_config_set *set)
  676. {
  677. if (set == NULL)
  678. return;
  679. perf_config_set__purge(set);
  680. free(set);
  681. }
  682. /*
  683. * Call this to report error for your variable that should not
  684. * get a boolean value (i.e. "[my] var" means "true").
  685. */
  686. int config_error_nonbool(const char *var)
  687. {
  688. pr_err("Missing value for '%s'", var);
  689. return -1;
  690. }
  691. void set_buildid_dir(const char *dir)
  692. {
  693. if (dir)
  694. scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir);
  695. /* default to $HOME/.debug */
  696. if (buildid_dir[0] == '\0') {
  697. char *home = getenv("HOME");
  698. if (home) {
  699. snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
  700. home, DEBUG_CACHE_DIR);
  701. } else {
  702. strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
  703. }
  704. buildid_dir[MAXPATHLEN-1] = '\0';
  705. }
  706. /* for communicating with external commands */
  707. setenv("PERF_BUILDID_DIR", buildid_dir, 1);
  708. }