config.c 15 KB

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