config.c 17 KB

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